How to use .getElementbyId("") in JS? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to use .getElementbyId("") in JS?

I am confused.

16th Jun 2021, 6:02 AM
Anas Dharar
Anas Dharar - avatar
11 Answers
+ 5
Anas Dharar First set 'id' attribute to your desired element. Example: <div id='box'>Div</div> Next, access element by it's id. var box=document.getElementById('box'); And, work with it. Example: //changing innerHtml of div box.innerHTML="Div Box"; //Now innerhtml of div was changed https://www.sololearn.com/learn/JavaScript/2753/
16th Jun 2021, 6:19 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 4
getElementById do not take the 's' as it return only one element... ByClassName take the 's' as returning an array-like of possibly many elements ^^
16th Jun 2021, 7:10 AM
visph
visph - avatar
+ 3
you have completed the css course, so you may be able to do it yourself: the only rule to be aware is the conversion of css kebab-case properties to js camelCase properties: background-color => backgroundColor
16th Jun 2021, 7:13 AM
visph
visph - avatar
+ 2
you forgot a 's' at Element end in function name ;)
16th Jun 2021, 7:06 AM
visph
visph - avatar
+ 2
document.getElementsByClassName
16th Jun 2021, 7:07 AM
visph
visph - avatar
+ 1
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ Thanks....Can I change the color of text with this??? If yes, please give the code
16th Jun 2021, 6:54 AM
Anas Dharar
Anas Dharar - avatar
+ 1
Yes. You can change it with css style color property. attach it in your Js. box.style.color="red"; refer it. https://www.sololearn.com/learn/JavaScript/2754/
16th Jun 2021, 6:58 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
+ 1
https://code.sololearn.com/WSee8LVQYHku/?ref=app https://code.sololearn.com/W2AmQTsVh9Z7/?ref=app
16th Jun 2021, 7:11 AM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
0
Thanks a lot visph & ˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ .....can you give a few examples that change Css elements like font and alignment??
16th Jun 2021, 7:09 AM
Anas Dharar
Anas Dharar - avatar
16th Jun 2021, 7:13 AM
Anas Dharar
Anas Dharar - avatar