Javascript examples for jQuery Method and Property:attr
The attr() method sets or returns attributes and values of the selected elements.
$(selector).attr(attribute, value, function(index, currentvalue));
Parameter | Description |
---|---|
attribute | Specifies the name of the attribute |
value | Specifies the value of the attribute |
function(index,currentvalue) | Specifies a function that returns the attribute value to set |
The following code shows how to set the width attribute of an image:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("img").attr("width", "500"); });//from www . j a v a 2s . c o m }); </script> </head> <body> <img src="http://java2s.com/resources/a.png" alt="message" width="300" height="213"><br> <button>Set the width attribute of the image</button> </body> </html>