How can i get a checked Checkbox title value in javascriptjquery? - Stack Overflow

I want to get chekbox title value. How can i do that ?<asp:CheckBox ID="chkSingle" runat=&

I want to get chekbox title value. How can i do that ?

<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" title='<%#Eval("CarServiceFormInvoiceRecordID") %>' />

My func doesn't work..

function Checked(cb) {
            if (cb.checked) {
                alert(cb.title);
...

I want to get chekbox title value. How can i do that ?

<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" title='<%#Eval("CarServiceFormInvoiceRecordID") %>' />

My func doesn't work..

function Checked(cb) {
            if (cb.checked) {
                alert(cb.title);
...
Share asked May 26, 2013 at 15:25 ErdoganErdogan 1,01215 silver badges27 bronze badges 19
  • 2 use cb.getAttribute('title') – Tamil Selvan C Commented May 26, 2013 at 15:26
  • @TamilSelvan How is this different from OP using cb.title? – A. Wolff Commented May 26, 2013 at 15:31
  • @Ted How is this different from OP using cb.checked? – A. Wolff Commented May 26, 2013 at 15:31
  • "My func doesn't work." Which means? Any error or what? – A. Wolff Commented May 26, 2013 at 15:31
  • 1 Maybe <asp:Checkbox /> will change the title attribute to <label /> tag. – user1823761 Commented May 26, 2013 at 15:35
 |  Show 14 more ments

5 Answers 5

Reset to default 2

use this

$('#chkSingle').attr('title')

to ensure that checkbox is checked

$('#chkSingle[type="checkbox"]').filter(function(){return $(this).is(':checked')}).attr('tile')

Based on these ments:

[NOX] - Can you show us the GENERATED code of your ?
[Ahmet] - <input id="chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);">

The title attribute is not generated in the output. Why? I don't know really. Let's check it step by step.

First, change your checkbox to this one:

<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" title='HELLO' />

As you see, I changed the value of title attribute. Check the output, if you haven't any title attribute, try to change it to something like data-title:

<asp:CheckBox ID="chkSingle" runat="server" onclick="Checked(this);" data-title='HELLO' />

Now check your generated code, it must be something like this (I hope so):

<input id="chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);" data-title="Hello">

If in generated code, the attribute data-title is exists, then you succeed. Now you must change your function to get this attribute:

alert(cb.getAttribute('data-title'));

UPDATE

As you ment, the generated code is:

<span data-title="HELLO"><input id="ctl00_c1_RadGrid1_ctl00_ctl04_chkSingle" type="checkbox" name="ctl00$c1$RadGrid1$ctl00$ctl04$chkSingle" onclick="Checked(this);" /></span>

So, the attribute you attach to the <asp:Checkbox /> became to an span tag. So you must change your function to something like this:

function Checked(cb) {
    var $input = $(cb);
    var $span = $input.closest('span');
    var title = $span.attr('data-title');

    if ($input.is(':checked')) {
        alert(title);
    }
}
  • Check the Working jsFiddle Demo.
  • I think you can put back data-title to title attribute again.

jQuery:

if ($(this).is(':checked')) {
  alert($(this).attr('title'));
}

Javascript:

change you call from Checked(this); to Checked(this.id);

and you function to:

function Checked(checkId) {
  var checkbox = document.getElementById(checkId);
  if(checkbox.checked) {
    alert(checkId);
    ....

Sorry, getting caught up with some of the ments here, I oversaw your actual problem. You need to have a look at the markup generated and see how the label for the checkbox is generated. Paste it here and I'll tell you how to access the value.

you may try like this

function Checked(cb) {
            if (cb.checked) {
                alert(cb.getAttribute('title'));
   }
}

you may try like that ::

$(':checked').prop('title', function(k) {
        tit[k]= $(this).attr('title');
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855657a4597386.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信