0

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);
...
19
  • 2
    use cb.getAttribute('title') Commented May 26, 2013 at 15:26
  • @TamilSelvan How is this different from OP using cb.title? Commented May 26, 2013 at 15:31
  • @Ted How is this different from OP using cb.checked? Commented May 26, 2013 at 15:31
  • "My func doesn't work." Which means? Any error or what? Commented May 26, 2013 at 15:31
  • 1
    Maybe <asp:Checkbox /> will change the title attribute to <label /> tag. Commented May 26, 2013 at 15:35

5 Answers 5

2

use this

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

to ensure that checkbox is checked

$('#chkSingle[type="checkbox"]').filter(function(){return $(this).is(':checked')}).attr('tile')
Sign up to request clarification or add additional context in comments.

2 Comments

How is this different from OP using cb.title? BTW, using property title is still better
The ID attribute inside ASP Web Controls, isn't exactly what you get in your HTML output, so your selector ($('#chkSingle')) will always return an empty matched set.
1

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

3 Comments

Thank you ! But i can't get the data-title value because 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> How can i get span value ?
@Ahmet Thanks you can use jQuery in your project, check this jsFiddle.
You are King.. Thank you so much
1

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.

6 Comments

No, why use jquery when js expose natively properties?
@roasted - I assumed he wanted a jquery solution due to the jquery tag.
I see a javascript tag too. BTW, why making code slower using jquery when you can access direct property using js?
@roasted - added a javascript solution, too. hope you're happy now
i don't think the problem come from here, i think OP is doing something wrong somewhere else. But ya, i'm happy when not seeing useless jquery code :) {i don't mean your previous answer was useless}
|
0

you may try like this

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

1 Comment

How is this different from OP using cb.title?
0

you may try like that ::

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

Comments

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.