jquery get attribute value – The jQuery attr() method is used to get attribute values. The attr() method sets or returns attributes and values of the selected elements.
jquery: get value of custom attribute
$("input[placeholder]").val($("input[placeholder]").attr("placeholder"));
jquery get attribute value
Contents
The name of the attribute to get. simple The .attr() method retrives the attribute value for only the first html element in the matched data set.
get attribute value jquery
var some_var = $( some_jquery_selector ).attr( 'some_attribute_name' );
How to get HTML attribute values in JQuery?
index.html
<html> <head> <title>jQuery get attribute example - www.pakainfo.com</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var title = $("em").attr("title"); alert(title); $("#pakanew").text(title); }); </script> </head> <body> <div> <em title="Welcome To Pakainfo.com">Simple jQuery get attributes</em> <p id="myid">getting my title attribute- success.</p> <div id="pakanew"></div> </div> </body> </html>
how to get attribute value in jquery
Example
/* html source code */ <a data-id="98255">link</a> /* js */ $(this).attr("data-id") // returns string "98255" $(this).data("id") // returns number 98255 (jQuery >= 1.4.3 only)
jQuery Set Attribute
index.html
<html> <head> <title>jQuery set attribute example - www.pakainfo.com</title> <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#pakanew").attr("alt", "Setting New Image"); }); </script> </head> <body> <div> <img id="pakanew" src="welcome.png" alt="Welcome New Pakainfo" /> </div> </body> </html>
How to get attribute value in javascript?
I hope you get an idea about jquery get attribute value.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโt forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.