0

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");
               }
            }
      });
}); 
3
  • you are using if(c = "Active"). You should use if(c == "Active"). is it typo mistake ? Commented Oct 7, 2013 at 7:58
  • You have some non-sense bits on your code. Commas finishing lines? Comparing a value using = instead of ==? Commented Oct 7, 2013 at 7:59
  • simple beginner mistakes unfortunately, typos and forgetfulness Commented Oct 7, 2013 at 8:05

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.