0

In Jquery Can I append and add attr like: for this:

  <li class="test" ln="7" >

How do I get the value of r into this:

$('#box input[name= _def]').val( JUNK) + "VALUE OF ln";

Can I get the value of custom attribute 'ln' appended after the val ?

1
  • for what purpose do you intend to use the attr ? Commented Dec 13, 2012 at 3:25

2 Answers 2

2

Use a data- attribute to hold your data:

<li class="test" data-ln="7" >

You can add the attribute with jQuery using .data():

$li.data('ln', 7);

And you can use .data() to extract it (and automatically convert common data types):

var ln = $li.data('ln');
Sign up to request clarification or add additional context in comments.

7 Comments

Is this supported across all the browsers?
@user1022521: Sure, I don't see why not. I think jQuery resorts to using setAttribute() for browsers that don't support HTML5 data attributes.
It is with the HTML5 shiv, or jQuery fixes it for you via .data() defaults.
My problem is not adding the attribute - but getting the value of this attribute in this line $('#box input[name= _def]').val( JUNK) + "VALUE OF ln";
can I get the value of the atribute after .val in jquery? even if I add as a data attribute I am not able to add this value of attribute to the line I wanted
|
1

Better to use HTML-5 data attributes for validated HTML..

This is supported by all the browsers.

<li class="test" data-ln="7" >

var val = $('li.test').data('ln');

For such cases you can use .data() method to extract the value.

If you still want to follow the same syntax that you are using which is not recommended, you can use the getAttribute method

   var val = document.getElementsByClassName('test').getAttribute('ln');

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.