Erdogan
Erdogan

Reputation: 990

How can i get a checked Checkbox title value in javascript/jquery?

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);
...

Upvotes: 0

Views: 8688

Answers (5)

anilam
anilam

Reputation: 892

you may try like that ::

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

Upvotes: 0

user1823761
user1823761

Reputation:

Based on these comments:

[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 comment, 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);
    }
}

Upvotes: 1

Ted
Ted

Reputation: 4067

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 comments 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.

Upvotes: 1

Sri Tirupathi Raju
Sri Tirupathi Raju

Reputation: 819

use this

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

to ensure that checkbox is checked

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

Upvotes: 2

sAnS
sAnS

Reputation: 1163

you may try like this

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

Upvotes: 0

Related Questions