Getting checkbox in JavaScript or JQuery value while OnClick

I had a checkbox I need to set a checkbox value during validation. either JavaScript or jQuery. I have posted my ASPX code below. which should work in IE. For now I am trying to show it as "on" and not a value.

ASPX code:

<asp:TemplateField>
    <HeaderTemplate>
        <asp:CheckBox ID="CHK_STY_ALL" 
                      runat="server" 
                      onclick="javascript:Selectallcheckbox(this);" />
    </HeaderTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="CHK_STY" 
                      runat="server" 
                      onclick="javascript:SelectallColorsForStyle(this,value);" 
                      CssClass="checkboxselection" 
                      Text='<%#Eval("STY_NBR")%>' />
    </ItemTemplate>
</asp:TemplateField>

      

JavaScript and JQuery:

<script language="javascript" type="text/javascript">
    function Selectallcheckbox(val) {
        if (!$(this).is(':checked')) {
            $('input:checkbox').prop('checked', val.checked);
        } else {
            $("#chkroot").removeAttr('checked');
        }
    }
</script>
<script type="text/javascript">
    function SelectallColorsForStyle(e,val) {
        var IDValue = $(e).attr('id');

        var StyleNumber = document.getElementById(IDValue).value;
        alert(StyleNumber);
    }
</script>

      

enter image description here

Displays the value as "on" rather than the original value displayed away from the checkbox.

+3


source to share


2 answers


ASP.Net CheckBox is rendered as input and label .

<span class="checkboxselection">
    <input id="GridView1_CHK_STY_0" 
        type="checkbox" 
        name="GridView1$ctl02$CHK_STY" 
        onclick="javascript: SelectallColorsForStyle(this, value);" />
    <label for="GridView1_CHK_STY_0">100005</label>
</span>

      

Therefore, you need to select the sibling label text .



<script type="text/javascript">
    function SelectallColorsForStyle(e, val) {
        var label = $(e).siblings("label");
        alert(label.text());
    }
</script>

      

enter image description here

+3


source


I don't know how to use asp, but this script will work for you.

<input type="checkbox" id="check-demo" value="some value" onchange="Selectallcheckbox(this)"/>

<script>
function Selectallcheckbox(element){
  alert(element.value);
}
</script>

      



If you are using jQuery then below will be the snippet below

$("#check-demo").click(function(){
 if($(this).is(":checked")) {
   alert($(this).val());     
   }
});

      

+2


source







All Articles