Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript how create my buttons inside the div with display grid?

i want my button created inside the div with display grid. so you have 8 buttons in every row. its for my Javascript homework.
HTML:
<!DOCTYPE html>
<html lang="fa" dir="rtl">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test exc10</title>
    <style>
        .btnn {
            background: white;
            border: 1px solid black;
            border-radius: 10px;
            width: 80px;
            margin: 5px;
            display: flex;
            align-items: center;
        }
        .container {
            display: grid;
            grid-template-columns: auto auto auto auto auto auto auto auto;
           
        }
    </style>
</head>

<body>
    <p>لطفا زمان مورد نظر برای مراجعه به مطب را انتخاب نمایید :</p>
    <div class="container"></div>
    <script>
        for (let h = 8; h <= 20; h++) {
            for (let m = 0; m < 60; m += 15) {
                let time = h;
                time = time + ":" + m;
                if (m == 0) {
                    time += 0;
                }
                const btn = document.createElement("button");
                btn.setAttribute("class", "btnn");
                btn.innerHTML = ("<br>" + "<input type='radio' name='rad'>" + time);
                document.body.appendChild(btn);
            }
        }
    </script>
</body>
</html>
 
Last edited by a moderator:
Hi there.
You want the buttons to be placed in the div element with the container class, but you're appending them to the body with this line of code:
JavaScript:
document.body.appendChild(btn);

What you have to do is append the buttons to the .container element. Instead of selecting the body (with document.body), select the div element using document.querySelector(".container"):
JavaScript:
document.querySelector(".container").appendChild(btn);
 
Hi there.
You want the buttons to be placed in the div element with the container class, but you're appending them to the body with this line of code:
JavaScript:
document.body.appendChild(btn);

What you have to do is append the buttons to the .container element. Instead of selecting the body (with document.body), select the div element using document.querySelector(".container"):
JavaScript:
document.querySelector(".container").appendChild(btn);
thanks for your help buddy. it worked very well. i guess i didnt know about the ( . ) before ".container" class.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom