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);     }) }); 

jsfiddle example


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -