javascript - 'href' Not Showing Output on Click -
im trying have href link expand/display text when clicked when click nil happens.
when run html code can click on link not show text reason. thought why?
heres code:
<html> <a href="#1" onclick="$('divid').toggle();">click expand</a> <div id="divid" style="display: none;">this expanded</div> </html>
i'm trying maintain code short possible above code have repeated hundreds of times each link.
assuming you're using jquery, using css selector incorrectly. line should this:
<a href="#1" onclick="$('#divid').toggle();">click expand</a>
the #
in #divid
represents element id
of divid
, whereas using divid
search divid
tags (something <divid></divid>
)
see here more documentation on id selector , here's list of css selectors can use, including the element selector understand why previous code didn't work.
you can combine css selectors narrow selection in future, although it's not much necessary id selector:
<a href="#1" onclick="$('div#divid').toggle();">click expand</a>
and if absolutely insist on not using jquery:
<a href="#1" onclick="if (document.getelementbyid('divid').style.display == 'none') document.getelementbyid('divid').style.display = 'block'; else document.getelementbyid('divid').style.display = 'none';">click expand</a>
or breaking out own function:
<script> function toggleelementbyid(id) { if (document.getelementbyid(id).style.display == 'none') { document.getelementbyid(id).style.display = 'block'; } else { document.getelementbyid(id).style.display = 'none'; } } </script> <a href="#1" onclick="toggleelementbyid('divid');">click expand</a>
javascript html
No comments:
Post a Comment