Click here to Skip to main content
15,881,794 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi!

I want to change the behavior of a button on select change, it is working on select change of another an option chosen which is different of the select text in the dropdownlist .

In brief what I want is to disabled a button when you select select as an option not other...
once I select USA or Russia it is working fine but once I select Select again it is not working means it is not disabled the button.
This is the code I am using:

HTML:
======
<select id="ddl">
<option>Select</option>
<option>USA</option>
<option>Russia</option>
</select>
<input type='button' value='Send' id='SendId' disabled/>

C#
JQuery:
======
$(function(){
    $('#ddl').on("change", function(){
    var getSelectedtext= $("#ddl option:selected").text();
    var getFirsttext = $("#ddl option:first-child").text();
    if(getFirsttext)
    {
        $('#SendId').prop("disabled", false);
        alert(getSelectedtext);
    }
    else{
        alert(getSelectedtext);
        $('#SendId').prop("disabled", true);
    }
   });
});


you can also test it using jsfiddle I created
Can someone help!
Posted
Updated 23-Oct-13 3:45am
v3

1 solution

Try this:

JavaScript
$(function(){
    $('#ddl').on("change", function(){
    var getSelectedtext= $("#ddl option:selected").val();
    var getFirsttext = $("#ddl option:first-child").val();

    var isDefault = $("#ddl option:selected").text() == $("#ddl option:first-child").text() ? true : false;

    console.log(isDefault);
    if(!isDefault)
    {
        $('#SendId').prop("disabled", false);
        alert(getSelectedtext);
    }
    else{
        alert(getSelectedtext);
        $('#SendId').prop("disabled", true);
    }
    //alert(gettext);
    //alert(getFirsttext);
});
});



Remark: good idea to post a fiddle;)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900