javascript - jQuery: is it possible to make ID/class-less elements change on click? -


example css:

div {   background-color: green; } 

example html:

<div>apple 1</div> <div>apple 2</div> <div>apple 3</div> 

is possible bind jquery clicks these, clicked div change background color blue, , others revert default color?

i'm thinking like:

$("div").click(function() {    // first change default color    $("div").each( background-color: green; }    // change clicked div blue    how clicked element in here dont know. }); 

use $(this) refer clicked element:

    $("div").click(function() {         // first change default color         $("div").css('background-color', 'green')         // change clicked div blue         $(this).css('background-color','blue');      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>    <div>apple 1</div>  <div>apple 2</div>  <div>apple 2</div>

further reading:

jquery's this: demystified - remy sharp


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -