Friday, 15 January 2010

javascript - Trying to hide a div if the heading of the page = "x" -



javascript - Trying to hide a div if the heading of the page = "x" -

i'm trying tweeking of wordpress theme, , hide div on pages javascript, code have come is:

if(document.getelementsbyclassname("page-title") === "new arrivals"){ document.getelementbyid("pull_9").style.visibility="hidden"; }else if(document.getelementsbyclassname("page-title") === "sale"){ document.getelementbyid("pull_9").style.visibility="hidden"; }else{ document.getelementbyid("pull_9").style.visibility="visible"; };

but not working, know getelementbyclassname() not supper supported yet these requirement.

can see why not working? simple (i'm new javascript) cant theoretically see why shouldn't work.

are new arrivals & sale content of elements?

if page-title refers <title> tag, can utilize that

document.queryselector('title').textcontent

if page-title refers 1 item or first item can try:

document.queryselector('.page-title').textcontent

alternatively ...

var elem = document.getelementsbyclassname('page-title'); (var = 0, len = elem.length; < len; i++) { if (elem[i].textcontent === 'new arrivals' || elem[i].textcontent === 'sale') { document.getelementbyid('pull_9').style.visibility = 'hidden'; break; } }

or ....

for (var = 0, len = elem.length; < len; i++) { if (elem[i].textcontent.match(/new arrivals|sale/)) { document.getelementbyid("pull_9").style.visibility = 'hidden'; break; } }

there many ways accomplish same objective.

if 1 element code refers ... example

if (document.queryselector('.page-title').textcontent.match(/new arrivals|sale/)) { document.getelementbyid('pull_9').style.visibility = 'hidden'; }

javascript if-statement visibility

No comments:

Post a Comment