JQUERY - ATTRIBUTESSome of the most basic components we can manipulate when it comes to DOM elements are the properties and attributes assigned to those elements. Most of these attributes are available through JavaScript as DOM node properties. Some of the more common properties are:
<img id="myImage" src="image.gif" alt="An image"
class="someClass" title="This is an image"/> In this element's markup, the tag name is img, and the markup for id, src, alt, class, and title represents the element's attributes, each of which consists of a name and a value. jQuery gives us the means to easily manipulate an element's attributes and gives us access to the element so that we can also change its properties. Get Attribute Value The attr() method can be used to either fetch the value of an attribute from the first element in the matched set or set attribute values onto all matched elements. Example Following is a simple example which fetches title attribute of <em> tag and set <div id="divid"> value with the same value:
<html>
<head> <title>the title</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var title = $("em").attr("title"); $("#divid").text(title); }); </script> </head> <body> <div> <em title="Bold and Brave">This is first paragraph.</em> <p id="myid">This is second paragraph.</p> <div id="divid"></div> </div> </body> </html> This will produce the following result:
This is first paragraph.
This is second paragraph. Bold and BraveSet Attribute Value The attr(name, value) method can be used to set the named attribute onto all elements in the wrapped set using the passed value. Example Following is a simple example which set src attribute of an image tag to a correct location:
<html>
<head> <title>the title</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#myimg").attr("src", "/images/jquery.jpg"); }); </script> </head> <body> <div> <img id="myimg" src="/wongpath.jpg" alt="Sample image" /> </div> </body> </html> Applying Styles The addClass( classes ) method can be used to apply defined style sheets onto all the matched elements. You can specify multiple classes separated by space. Example Following is a simple example which sets class attribute of a para <p> tag:
<html>
<head> <title>the title</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("em").addClass("selected"); $("#myid").addClass("highlight"); }); </script> <style> .selected { color:red; } .highlight { background:yellow; } </style> </head> <body> <em title="Bold and Brave">This is first paragraph.</em> <p id="myid">This is second paragraph.</p> </body> </html> This will produce the following result:
This is first paragraph.
This is second paragraph. Attribute Methods Following table lists down few useful methods which you can use to manipulate attributes and properties:
The attr( properties) method set a key/value object as properties to all matched elements. Syntax Here is the simple syntax to use this method:
selector.attr({property1:value1, property2:value2})
Parameters Here is the description of all the parameters used by this method:
Following example would change the properties of an image tag:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("img").attr({ src: "/images/jquery.jpg", title: "jQuery", alt: "jQuery Logo" }); }); </script> </head> <body> <div class="divcl" id="divid"> <p>Following is the logo of jQuery</p> <img src="Wrong SRC" title="None" alt="None" /> </div> </body> </html> attr( key, func ) Method The attr( key, func ) method sets a single property to a computed value, on all matched elements. Syntax Here is the simple syntax to use this method:
selector.attr( key, func )
Parameters Here is the description of all the parameters used by this method:
The following example would create border for each table:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("table").attr("border", function(arr) { return arr; }) }); </script> </head> <body> <table> <tr><td>This is first table</td></tr> </table> <table> <tr><td>This is second table</td></tr> </table> <table> <tr><td>This is third table</td></tr> </table> </body> </html> This would produce the following result:
removeAttr( name ) Method The removeAttr( name ) method removes an attribute from each of the matched elements. Syntax Here is the simple syntax to use this method:
selector.removeAttr( name )
Parameters Here is the description of all the parameters used by this method:
Following example would remove border from each table:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("table").removeAttr("border"); }); </script> </head> <body> <table border="2"> <tr><td>This is first table</td></tr> </table> <table border="3"> <tr><td>This is second table</td></tr> </table> <table border="4"> <tr><td>This is third table</td></tr> </table> </body> </html> This would produce the following result:
This is first table
This is second table This is third table hasClass( class ) Method The hasClass( class ) method returns true if the specified class is present on at least one of the set of matched elements otherwise it returns false. Syntax Here is the simple syntax to use this method:
selector.hasClass( class )
Parameters Here is the description of all the parameters used by this method:
Following example would check which para has class red:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("#result1").text( $("p#pid1").hasClass("red") ); $("#result2").text( $("p#pid2").hasClass("red") ); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="red" id="pid1">This is first paragraph.</p> <p class="green" id="pid2">This is second paragraph.</p> <div id="result1"></div> <div id="result2"></div> </body> </html> This would produce the following result: This is first paragraph.This is second paragraph.true false removeClass( class ) Method The removeClass( class ) method removes all or the specified class(es) from the set of matched elements. Syntax Here is the simple syntax to use this method:
selector.removeClass( class )
Parameters Here is the description of all the parameters used by this method:
Following example would remove class red from the first para:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("p#pid1").removeClass("red"); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="red" id="pid1">This is first paragraph.</p> <p class="green" id="pid2">This is second paragraph.</p> </body> </html> This would produce the following result:
This is first paragraph.
This is second paragraph. toggleClass( class ) Method The toggleClass( class ) method adds the specified class if it is not present, removes the specified class if it is present. Syntax Here is the simple syntax to use this method:
selector.toggleClass( class )
Parameters Here is the description of all the parameters used by this method:
Following example would remove a class with one click and in second click it would again add the same class:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $("p#pid").click(function () { $(this).toggleClass("red"); }); }); </script> <style> .red { color:red; } </style> </head> <body> <p class="green">Click following line to see the result</p> <p class="red" id="pid">This is first paragraph.</p> </body> </html> With the first click it would generate following result, in the second click it would revert to its original form as shown above:
Click the following line to see the result
This is first paragraph. html( ) Method The html( ) method gets the html contents of the first matched element. This property is not available on XML documents but it works for XHTML documents. Syntax Here is the simple syntax to use this method:
selector.html( )
Parameters Here is the description of all the parameters used by this method:
Following example would get HTML content of first paragraph and would display it in second paragraph. Please check description of html(val) method as well.
<html>
<head> <title>The Selecter Example <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("p").html(); $("p#pid2").html( content ); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="green" id="pid1">This is first paragraph. <p class="red" id="pid2">This is second paragraph. </body> </html> This would produce the following result.
This is first paragraph.
This is first paragraph. html( val ) Method The html( val ) method sets the html contents of every matched element. This property is not available on XML documents but it works for XHTML documents. Syntax Here is the simple syntax to use this method:
selector.html( val )
Parameters Here is the description of all the parameters used by this method:
Following example would get HTML content of first paragraph and would display it in second paragraph. Please check description of html() method as well.
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("p").html(); $("p#pid2").html( content ); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="green" id="pid1">This is first paragraph.</p> <p class="red" id="pid2">This is second paragraph.</p> </body> </html> This would produce the following result.
This is first paragraph.
This is first paragraph. text( ) Method The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents. Syntax Here is the simple syntax to use this method:
selector.text( )
Parameters Here is the description of all the parameters used by this method:
Following example would find the text in the first paragraph stripping out the html, then set the html of the second paragraph to show it is just text
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("p#pid1").text(); $("p#pid2").html(content); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="green" id="pid1">This is <i>first paragraph</i>.</p> <p class="red" id="pid2">This is second paragraph.</p> </body> </html> This would produce the following result.
This is first paragraph.
This is first paragraph. text( val ) Method The text( val ) method sets the text contents of all matched elements. This method is similar to html( val ) but escapes all HTML entities. Syntax Here is the simple syntax to use this method:
selector.text( val )
Parameters Here is the description of all the parameters used by this method:
Following example would set the HTML content of the first paragraph in the second paragraph but it escapes all the HTML tag.
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("p#pid1").html(); $("p#pid2").text(content); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <p class="green" id="pid1">This is <i>first paragraph</i>.</p> <p class="red" id="pid2">This is second paragraph.</p> </body> </html> This would produce the following result.
This is first paragraph.
This is <i>first paragraph</i>. val( ) Method The val( ) method gets the input value of the first matched element. Syntax Here is the simple syntax to use this method:
selector.val( )
Parameters Here is the description of all the parameters used by this method:
Following example would set the HTML content of the first input box in the second paragraph:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("input").val(); $("p#pid2").text(content); }); </script> <style> .red { color:red; } .green { color:green; } </style> </head> <body> <input type="text" value="First Input Box"/> <input type="text" value="Second Input Box"/> <p class="green" id="pid1">This is <i>first paragraph</i>.</p> <p class="red" id="pid2">This is second paragraph.</p> </body> </html> This would produce the following result.
This is first paragraph. This is second paragraph. The val( val ) method sets the input value of every matched element. If this method is called on radio buttons, checkboxes, or select options then it would check, or select them at the passed value. Syntax Here is the simple syntax to use this method:
selector.val( val )
Parameters Here is the description of all the parameters used by this method:
Following example would set the value attribute of the second input with the value content of the first input:
<html>
<head> <title>The Selecter Example</title> <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { var content = $("input").val(); $("input").val( content ); }); </script> </head> <body> <input type="text" value="First Input Box"/><br/> <input type="text" value="Second Input Box"/> </body> </html> This would produce the following result.
Examples Similar to above syntax and examples, the following examples would give you understanding on using various attribute methods in different situation:
|