vmars316 5 Posted October 24, 2020 Report Share Posted October 24, 2020 Tia ; I want to change all fontWeight of <p and <div>> to bold . <!DOCTYPE html> <html> <head> <title>Bold Text Demo</title> </head> <body> <p> This is a sample paragraph </p> <button id="button"> Make Bold </button> <div>This is a sample div</div> <script> //Get the button element var btn = document.getElementById('button'); //Get paragraph element var makeBold = document.querySelectorAll(p , div); //Define an click event listener on button btn.addEventListener('click', function() { //Change the font weight style to 700 makeBold.style.fontWeight = '700'; }); </script> </body> </html> How can I do that ? Thanks for your Help... Quote Link to post Share on other sites
dsonesuk 921 Posted October 25, 2020 Report Share Posted October 25, 2020 (edited) Wrap in quotes 'p, div', then use for loop with makeBold.length to loop through collection from querySelectorAll(); to apply required style. Edited October 25, 2020 by dsonesuk 1 Quote Link to post Share on other sites
vmars316 5 Posted October 25, 2020 Author Report Share Posted October 25, 2020 Thanks dsonesuk ; Like this: Quote <script> //Get the button element var btn = document.getElementById('button'); //Get paragraph element var makeBold = document.querySelectorAll('p , div'); //Define an click event listener on button btn.addEventListener('click', function() { for (i = 0; i < makeBold.length; i++) { makeBold[i].style.fontWeight = '700'; } }); </script> Thanks ! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.