最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Adding a square in javascript - Stack Overflow

matteradmin6PV0评论

I want to create a square in JavaScript. I have made a class in CSS with square. I want to create something like a <div class="square></div> so that the square is displayed when I click on the button. I have tried using createElement and passing the statement as innerHTML. However that didn't seem to work. Can someone help me in suggesting what I am missing? I am trying to create a matrix of squares using the number input by the user

<!DOCTYPE html>

<html lang="en" xmlns="">
<head>
<!-- <meta charset="utf-8" /> -->
<title>Form Validation</title>
<script>
    function validateForm()
    {
        var x = document.forms["MyForm"]["Numval"].value;

        if ((!x.match(/^\d+/)) || ((x.length) > 1)) {
            alert("Please only one enter numeric character (Allowed input:1-9)");
        }
        else {
            for (i = 0; i < Number(x) ; i++) {
                for (j = 0; j < Number(x) ; j++) {
                    document.writeln(x);

                    var square = document.createElement('div');
                    square.className = 'squared';
                    document.body.appendChild(square)

                }
                document.writeln("</br>");
                }

        }

        return false;

    }
</script>



<style type="text/css">
.squared {
border: 2x solid black;
width: 50px;
height: 50px;
background:black;
color:black;
}
</style>

</head>
<body>
<form name="MyForm" onsubmit="return validateForm()" method="post" >


    Please enter a number between 1-9 : 
    <input type ="text" name="Numval"/>
   <input type="Submit" value="Submit" />


</body>
</html>

I want to create a square in JavaScript. I have made a class in CSS with square. I want to create something like a <div class="square></div> so that the square is displayed when I click on the button. I have tried using createElement and passing the statement as innerHTML. However that didn't seem to work. Can someone help me in suggesting what I am missing? I am trying to create a matrix of squares using the number input by the user

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3/1999/xhtml">
<head>
<!-- <meta charset="utf-8" /> -->
<title>Form Validation</title>
<script>
    function validateForm()
    {
        var x = document.forms["MyForm"]["Numval"].value;

        if ((!x.match(/^\d+/)) || ((x.length) > 1)) {
            alert("Please only one enter numeric character (Allowed input:1-9)");
        }
        else {
            for (i = 0; i < Number(x) ; i++) {
                for (j = 0; j < Number(x) ; j++) {
                    document.writeln(x);

                    var square = document.createElement('div');
                    square.className = 'squared';
                    document.body.appendChild(square)

                }
                document.writeln("</br>");
                }

        }

        return false;

    }
</script>



<style type="text/css">
.squared {
border: 2x solid black;
width: 50px;
height: 50px;
background:black;
color:black;
}
</style>

</head>
<body>
<form name="MyForm" onsubmit="return validateForm()" method="post" >


    Please enter a number between 1-9 : 
    <input type ="text" name="Numval"/>
   <input type="Submit" value="Submit" />


</body>
</html>
Share Improve this question edited Jan 10, 2013 at 4:04 Jack Smith asked Jan 10, 2013 at 3:40 Jack SmithJack Smith 111 gold badge1 silver badge3 bronze badges 3
  • 3 We can't help figure out what's wrong if you don't post your code. – Jason Commented Jan 10, 2013 at 3:41
  • Could you show us the code? – Anthony Forloney Commented Jan 10, 2013 at 3:41
  • Very vague, but many options. Probably easiest is to create a css class called "square". Then create an element and give it the class square. – Raekye Commented Jan 10, 2013 at 3:42
Add a ment  | 

2 Answers 2

Reset to default 1
var square = document.createElement('div');
square.className = 'square';
document.body.appendChild(square);

Here's the fiddle: http://jsfiddle/Ysw9n/

If you use jquery it's really a simple thing to do:

$("#your-button-id").click(function() { 
   $("#your-square-parent-id").append('<div class="your-square-class">&nbsp;</div>');
});

But you can do that without jquery as well as Joseph stated above. Anyways, hope it is useful.

Post a comment

comment list (0)

  1. No comments so far