jQuery Tutorial
attr("attribute_name")
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ var title = $("p:first").attr("title"); $("#divContent").text(title); }); </script> </head> <body> <p title="first main div">This is first paragraph content.</p> <div id="divContent"></div> </body> </html>Output:
<p title="first main div">This is first paragraph content.</p> <div id="divContent">first main div</div>
attr("attribute_name", "value")
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("img").attr("src", "wp-includes/images/images.png"); $("img").attr("alt", "codingpointer website"); }); </script> </head> <body> <img /> </body> </html>Output:
<img src="wp-includes/images/images.png" alt="codingpointer website" />
// properties are collection key/value objects. attr(properties)
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("img").attr({"src": "wp-includes/images/images.png", "alt": "codingpointer website"}); }); </script> </head> <body> <img /> </body> </html>Output:
<img src="wp-includes/images/images.png" alt="codingpointer website" />
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("a:first").prop("href", "javascript:alert('hi, thanks for visiting!');"); $("p").text($("a:first").prop("href")); }); </script> </head> <body> <a>webpage link.</a> <p></p> </body> </html>Output:
jQuery Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page