Ask Your Question
1

How can JavaScript be used to create rectangles containing text input boxes?

asked 2021-06-19 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-11-11 16:00:00 +0000

pufferfish gravatar image

There are different approaches to create rectangles containing text input boxes using JavaScript. One possible way is to use HTML and CSS to create the rectangle structure and add the input boxes dynamically with JavaScript. Here is an example:

HTML:

<div id="rectangle-container"></div>

CSS:

.rectangle {
  border: 1px solid #000;
  padding: 10px;
  margin-bottom: 10px;
}

JavaScript:

// Get a reference to the rectangle container
var rectangleContainer = document.getElementById("rectangle-container");

// Define the dimensions and text of the rectangles
var rectangles = [
  { width: "200px", height: "100px", text: "Rectangle 1" },
  { width: "300px", height: "150px", text: "Rectangle 2" },
  { width: "150px", height: "75px", text: "Rectangle 3" }
];

// Loop through the rectangles and create a div for each one
for (var i = 0; i < rectangles.length; i++) {
  var rectangle = rectangles[i];

  // Create the rectangle div and set its CSS properties
  var rectangleDiv = document.createElement("div");
  rectangleDiv.className = "rectangle";
  rectangleDiv.style.width = rectangle.width;
  rectangleDiv.style.height = rectangle.height;
  rectangleDiv.textContent = rectangle.text;

  // Create the input box and append it to the rectangle div
  var inputBox = document.createElement("input");
  inputBox.type = "text";
  rectangleDiv.appendChild(inputBox);

  // Add the rectangle div to the container
  rectangleContainer.appendChild(rectangleDiv);
}

This code creates three rectangles with different dimensions and text, and adds an input box to each one. The rectangles are displayed inside the rectangle-container div using the CSS styles defined for the .rectangle class. The resulting HTML structure will look like this:

<div id="rectangle-container">
  <div class="rectangle" style="width: 200px; height: 100px;">Rectangle 1<input type="text"></div>
  <div class="rectangle" style="width: 300px; height: 150px;">Rectangle 2<input type="text"></div>
  <div class="rectangle" style="width: 150px; height: 75px;">Rectangle 3<input type="text"></div>
</div>
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-19 11:00:00 +0000

Seen: 14 times

Last updated: Nov 11 '22