html - javascript function onclick must click twice -_- why so? -
i have page(it faq page web site) such when user click on div (e.g. #question01) there's new div appears below it. easy thing function, when click first time, nothing appears, click , there go. #faqanswer01 has "unhide" class set display:block; (.hide = display:none;)
so here 2 question :
1- why have click twice function executed?
2- how can fix code works after 1 click?
html code
<div id="faqcontainer"> <div id="question01" onclick="showanswer('faqanswer01','imgarrow01');">here's question?<img src="public/images/gt.png" class="imgarrow" id="imgarrow01"></div> <div id="faqanswer01" class="faqanswerdiv hide">bla bla bla</div> <div id="question02" onclick="showanswer('faqanswer02','imgarrow02');">another question here? <img src="public/images/gt.png" class="imgarrow" id="imgarrow02"></div> <div id="faqanswer02" class="faqanswerdiv hide">answer here.</div> </div>
my function:
function showanswer(idanswer , idimg) { if (document.getelementbyid(idimg).src == "http://www.cbifinance.org/public/images/gt.png") { document.getelementbyid(idimg).src = "http://www.cbifinance.org/public/images/gt90.png"; document.getelementbyid(idanswer).classname = 'faqanswerdiv unhide'; } else { document.getelementbyid(idimg).src = "http://www.cbifinance.org/public/images/gt.png"; document.getelementbyid(idanswer).classname = 'faqanswerdiv hide'; } }
try changing image.src
full url , should work.
the reason work src
public/images/gt.png
though when loads image finds correctly, src
still public/images/gt.png
.
so first time click, runs else statement, , sets src
full absolute url. click again , changed src
expected first time. same goes class
, adding hide
class image, has second time adds show
. code functioning correctly, if statement wrong.
Comments
Post a Comment