Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-5
rated 0 times [  2] [ 7]  / answers: 1 / hits: 32003  / 14 Years ago, thu, march 3, 2011, 12:00:00

I want to create the simplest bookmarklet for my browser.


javascript:document.getElementsByClassName('source').style.visibility='visible';

I have multiple div.source in my body. By default they are set to .source { display:none; } with css.


My console tells me: Uncaught TypeError: Cannot set property 'display' of undefined


When I click the bookmarklet all .source divs should be visible. What am I doing wrong here?


More From » element

 Answers
14

You might need to loop through the results, like this:



var divs = document.getElementsByClassName('source');
for(var i=0; i<divs.length; i++) {
divs[i].style.display='block'
}


And also as @ionoy mentioned, use display attribute. I hope that helps.



http://jsfiddle.net/erick/rb7bn/1/


[#93471] Wednesday, March 2, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
natalyah

Total Points: 371
Total Questions: 90
Total Answers: 105

Location: The Bahamas
Member since Wed, Apr 12, 2023
1 Year ago
natalyah questions
;