0

I want to get name string from data-rName into JQuery. Currently, it is showing undefined. This rName should returns Name string but not value.

View

  <tr id="row" data-rName="@dr["rName"]" >

Script

  $('#DetailsPartial').on('click', '#row', function () {                  
           var rName = $(this).data('rName');           
        });
6
  • What is $(this) in this context ? Is it the jQuery object for the row ? Commented Aug 5, 2018 at 23:46
  • Inspect the html you are generating - what does @dr["rName"] generate? Commented Aug 5, 2018 at 23:48
  • @Shyju, i updated Jquery portion. Commented Aug 5, 2018 at 23:48
  • @it is showing undefined now. This is string value coming from database. Commented Aug 5, 2018 at 23:49
  • And your <tr> element does not have a id="row" attribute. You need to show us the code to reproduce your issue. What is the element with id="row"? (and it also suggests you have duplicate id attributes which is invalid html) Commented Aug 5, 2018 at 23:49

1 Answer 1

2

html5 data attribute name should not contain upper case letters.

Here is the relevant part from the spec.

A custom data attribute is an attribute in no namespace whose name starts with the string "data-", has at least one character after the hyphen, is XML-compatible, and contains no uppercase ASCII letters.

So change your attribute name to all lower case

<tr data-rname="some value" id="row"><td></td></tr>

and

$('#DetailsPartial').on('click', '#row', function () {                  
    var rName = $(this).data('rname'); 
    alert(rName);          
});

You may also use dash in the attribute name. For example,

and

var rName = $("#hello").data("r-name");
Sign up to request clarification or add additional context in comments.

1 Comment

Solved!. Thank you alot.

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.