Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Switch case

I can't seem to get this to work in jquery. I am using this code as an alternative to having to define each button, however this does not work :/

http://jsfiddle.net/pufamuf/FV4jW/1/

Also, I was wondering if/how I can use more than one statement for each case. Thank you :)

like image 611
pufAmuf Avatar asked Mar 02 '26 09:03

pufAmuf


2 Answers

You need to add a break after each case in your switch statement. Updated JSFiddle here.

From Wikipedia:

break; is optional; however, it is usually needed, since otherwise code execution will continue to the body of the next case block.

Add a break statement to the end of the last case as a precautionary measure, in case additional cases are added later.

Updated Javascript:

 $("input[type='button']").click(function() {
   switch(this.id) {
     case 'buttonone': $("#content").html("Content changed"); break; //notice BREAK
     case 'buttontwo': $("#content").html("Content changed again"); break;
   }
 });
like image 59
ghayes Avatar answered Mar 05 '26 00:03

ghayes


$("input[type='button']").click(function() {
    switch (this.id) { 
    case 'buttonone':
        $("#content").html("Content changed");
        break;
    case 'buttontwo':
        $("#content").html("Content changed again");
        break;
    }
});
like image 42
thecodeparadox Avatar answered Mar 04 '26 22:03

thecodeparadox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!