javascript - Using $(this) when selecting a div -
i have series of divs of same class no ids. want change background of div when clicked. tested function , works fine. when access element this, not working. question how buttonpressed() work div clicked on?
html
<div id="navbar"> <div class="navitem" onclick="buttonpressed()"><p>membership</p></div> <div class="navitem" onclick="buttonpressed()"><p>certification</p></div> <div class="navitem" onclick="buttonpressed()"><p>foundation</p></div> <div class="navitem" onclick="buttonpressed()"><p>seminars</p></div> <div class="navitem" onclick="buttonpressed()"><p>councils</p></div> </div>
javascript
function buttonpressed() { var bgstring = "url('navbuttonpressed.png')"; $(this).css('backgroundimage',bgstring); }
since you're using jquery can (and should) remove inline event handling , use:
$( document ).ready(function() { $('.navitem').click(function(){ var bgstring = "url('navbuttonpressed.png')"; $(this).css('backgroundimage',bgstring); }) });
Comments
Post a Comment