How can I get the value of a custom attribute using javascript or jquery?
Like
<strong id="the id" original-title="I NEED THIS">
I've tried with .getAttribute() and attr()
..(Javascrupt & jQuery) without success any idea?
How can I get the value of a custom attribute using javascript or jquery?
Like
<strong id="the id" original-title="I NEED THIS">
I've tried with .getAttribute() and attr()
..(Javascrupt & jQuery) without success any idea?
Don't use space in id.
And adding custom attributes make your html invalid. Use data-attributes
instead:
<strong id="the_id" data-original-title="I NEED THIS">
$('#the_id').data('original-title');
http://jsbin.com/akoyut/2/edit
Change "the id" to "the_id".
You can do it using plain javascript:
document.getElementById("the_id").getAttribute("original-title");
Best Way to use like this:
jQuery(this).attr('original-title');
a var DateofEvent = $('[data-original-title=I NEED THIS]').val();
You can get the value by using the following syntax
$('#theid').attr('original-title');
If you must use spaces in an id, retrieve the element and attribute value like this:
$('[id="the id"]').attr([some attribute string]);
//or
$('#the\\ id').attr([some attribute string]);
For custom attributes best use the HTML5 data-[somelabel]
attributes, it's backward compatible and standardized. So in your case something like:
<strong id="the id" data-originalTitle="I NEED THIS">
Read more about data-attributes
$('#the<remove space from here>id').attr('original-title');
The following is an working example:
Javascript:
$(document).ready(function() {
var title = $("#the_id").attr("original-title");
}
Html:
<strong id="the_id" original-title="I NEED THIS"></strong>
I did this as I needed it in a foreach
$(this).data('original-title')