Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  166] [ 7]  / answers: 1 / hits: 44154  / 14 Years ago, sat, june 26, 2010, 12:00:00

Im trying to show/hide a div using jquery when a link gets clicked. I put this in my head section:



<script type=text/javascript> 
$(#attach_box).click(function {
$(#sec_box).show()
});
</script>


I have a link that looks like this:



<a href=# id=attach_box>+ Add a Postal Address (If Different)</a>


And a div that looks like this:



<div id=sec_box style=display: none;>
Hello world!!
</div>


This doesn't work and I can't figure out why. Any ideas?


More From » jquery

 Answers
163

You need to attach the click handler in the document.ready in order to make sure that the DOM has been loaded by the browser and all the elements are available:



<script type=text/javascript> 
$(function() {
$('#attach_box').click(function() {
$('#sec_box').show();
return false;
});
});
</script>


Also you forgot to put parenthesis () next to the anonymous function in the click handler.


[#96404] Tuesday, June 22, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
victorr questions
Fri, Nov 13, 20, 00:00, 4 Years ago
Sat, Jul 25, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;