1

How to add CheckBox Name to Div control when Check box Checked status true through Jquery

I've tried the below code. but not get the result.

ASP CODE:

<div align="center" id="chkBoxes">
    <asp:CheckBox ID="chbIceCream" Text ="Ice Cream" runat="server" />
    <asp:CheckBox ID="chbCake" Text ="Cake" runat="server" />
    <asp:CheckBox ID="chbChocolet" Text ="Cho colet" runat="server" />
</div>

<div id="ContentDiv">


</div>

Jquery CODE:

$(document).ready(function () {
    $('#chbIceCream').click(function () {
        debugger;
        var Text = $('#chbIceCream').text();
        if ($(this).attr('checked'))
            $('#ContentDiv').append(Text);
    });
});

If any one help in that please .

2 Answers 2

3

Set your control ClientIDMode="static" if you want to use server side id in javascript or use CliendID in javascript.

Using ClientIDMode="static"

<asp:CheckBox ID="chbIceCream" Text ="Ice Cream" runat="server" ClientIDMode="static" />

No change in jQuery selector for control id

$(document).ready(function () {
    $('#chbIceCream').click(function () {
        debugger;
        var Text = $('#chbIceCream').text();
        if ($(this).attr('checked'))
            $('#ContentDiv').append(Text);
    });
});

Using ClientID

$(document).ready(function () {
  $('#<%= chbIceCream.ClientID %>').click(function () {
    debugger;
    var Text = $('#chbIceCream').text();
    if ($(this).attr('checked'))

        $('#ContentDiv').append(Text);
   })
});
Sign up to request clarification or add additional context in comments.

12 Comments

i am using script for seperate js file @Adil
it populates this error @Adil 'Microsoft JScript runtime error: Syntax error, unrecognized expression: #<%= chbIceCream.ClientID %>'
Yes it should give error I have updated my answer you need to use first option of ClientIDMode="static"
still i got same Error @Adil
With ClientIDMode you do not need #<%= chbIceCream.ClientID %>'
|
0

use this as selector

 $('#<%= chbIceCream.ClientID %>').click(function(){
  ....

UPDATED

add ClientIDMode="static" in your html

1 Comment

it populates this error @bipen 'Microsoft JScript runtime error: Syntax error, unrecognized expression: #<%= chbIceCream.ClientID %>'

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.