alexandercannon
alexandercannon

Reputation: 543

Using different methods on a button based on its value

I have a series of buttons created from a database, they are set to active or disabled based on a value in the database.

I am trying to create a script that sees what value the button has and changes it to the other state.

My buttons

    <input type='button'
    name='activebutton'
    id='actbutton11'
    value='Active'
    class='activebutton'
    />

My script currently looks like this, it is currently unable to differentiated between active and disabled buttons.

I can also get it to change the buttons value but it doesn't change the value of C if the method is called again.

I want the user to be able to call the new appropriate method once a button has changed state.

$(document).ready(function(){
    $(".activebutton").click(function(){

   var c = $(this).val();
   //  alert(c)
   if(c = "Active")
            {
             var answer = confirm("Disable button?")
         if (answer)
             {
               var $inputElement = $(this),
               $("activebutton").val("Disabled");
             }
           }
   else
           {
               var answer = confirm("Activate button?")
               var $inputElement = $(this),
               {
                   $("activebutton").val("Active");
               }
            }
      });
}); 

Upvotes: 0

Views: 62

Answers (2)

Ajith S
Ajith S

Reputation: 2917

Here is the working code,

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <script>

            $(document).ready(function () {
                $(".activebutton").click(function(){
                    var answer;
                    var c = $(this).val();
                    if(c == "Active")
                    {
                        answer = confirm("Disable button?")
                        if (answer) {
                            $(".activebutton").val("Disabled");
                        }
                    } else if (c == "Disabled"){
                        answer = confirm("Activate button?");
                        if (answer) {
                            $(".activebutton").val("Active");
                        }
                    }
                });
            });
        </script>
    </head>

    <body>

        <input type='button'
               name='activebutton'
               id='actbutton11'
               value='Active'
               class='activebutton'
               />

    </body>
</html> 

Upvotes: 1

palaѕн
palaѕн

Reputation: 73906

You can do this:

$(document).ready(function () {
    $(".activebutton").click(function () {
        var $inputElement = $(this),
            c = $(this).val();

        if (c === "Active") {
            var answer = confirm("Disable button?");
            if (answer) $inputElement.val("Disabled");

        } else {
            var answer = confirm("Activate button?");
            if (answer) $inputElement.val("Active");
        }
    });
});

FIDDLE DEMO

Upvotes: 1

Related Questions