SetAttribute in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

SetAttribute in JS

I have writtena code, setAttribute not working there. Am I using it wrong.? https://code.sololearn.com/WlF8DQ5vNaol/?ref=app

8th Aug 2023, 7:45 PM
Khan
5 Answers
+ 8
You have called more than one element in your code, thats the error. For this reason you need to choose one in this way with adding [0]: document.getElementsByClassName("anchor")[0].setAttribute("href", "http://www.sololearn.com");
8th Aug 2023, 8:56 PM
JaScript
JaScript - avatar
+ 5
Yes Khan that can be look like as in this example: https://code.sololearn.com/WrvqlFKLSvdq/?ref=app
9th Aug 2023, 7:02 AM
JaScript
JaScript - avatar
+ 5
Khan you are welcome. Happy coding!
9th Aug 2023, 4:55 PM
JaScript
JaScript - avatar
+ 1
JaScript , So what if I have to change the href of all <a> tags? Do I need a loop for that?
9th Aug 2023, 3:19 AM
Khan
+ 1
JaScript , it's amazing. Thanks for your help
9th Aug 2023, 4:24 PM
Khan