html - Need to be able to target individual button using code that applies to all buttons -


when 1 of buttons clicked, need button change red background white text. needs work new buttons added later on. how can without individually adjusting each button?

here's code:

<!doctype html>  <html>     <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8" />      <title>page 2</title>      <style type="text/css">     /* styles go here */     img {         width:200px;          height:100px;          animation: widthchange 3s 2s;         -webkit-animation: widthchange 3s 2s;         -moz-animation: widthchange 3s 2s;         -0-animation: widthchange 3s 2s;       }      p {text-align:center}     button {margin:20px}     .stylized {         font-style: italic;         border-width: 5px;         border-color: yellow;         border-style: outset;     }      @-webkit-keyframes widthchange {         0%, 100% {width: 200px;}         50% {width: 400px;}     }     @-o-keyframes widthchange {         0%, 100% {width: 200px;}         50% {width: 400px;}        }     @-moz-keyframes widthchange {         0%, 100% {width: 200px;}         50% {width: 400px;}     }     @keyframes widthchange {         0%, 100% {width: 200px;}         50% {width: 400px;}      }      </style>      <script src="http://code.jquery.com/jquery.min.js"></script>     <script type="text/javascript">     $(function(){        // jquery methods go here...        $(document).ready(function() {         $('img').addclass("loaded");         $('img').addclass("loaded2");         $("#button1").click(function() {             $("button").addclass("stylized");             $("#button1").html("fancy button 1");             $("#button2").html("fancy button 2");             $("#button3").html("fancy button 3");         });        });      });     /* additional javascript goes here */     </script>   </head>    <body>     <img class = "image" src="elephant.jpg" alt="elephant"/>     <p><button id="button1">button 1</button><button id="button2">button 2</button><button id="button3">button 3</button></p>    </body> </html> 

use jquery selectors .on function. refer $(this) inside function change specific button's properties.

edit: must bind document though, if add new buttons can select in .on() functions selector parameter. so:

$(document).ready(function() {      $(document).on('click', "input[type='button']", function(e) {        // sample      if ($(this).prop('id') == "addb") {        $("#buttons").append("<input type='button' value='another button'><br>");        return;      }      // end sample code          $(this).css({        backgroundcolor: "red",        color: "#fff"      });      });    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <input type="button" value="add more buttons" id="addb">  <br>  <br>    <div id="buttons">    <input type="button" value="hello">    <br>  </div>


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -