February 2006
Intermediate to advanced
826 pages
63h 42m
English
Content preview from Web Design in a Nutshell, 3rd EditionBecome an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
Start your free trial



getAttribute()/setAttribute( )
You can collect the value of an element’s attributes using the getAttribute( ) method. Assuming the same (X)HTML as the example above, you could use getAttribute( ) to collect the value of the anchor’s href attribute and place it in a variable called href:
var href = document.getElementById( 'easy' ).getAttribute( 'href' );
The value returned by getAttribute( ) is the nodeValue of the attribute named as the argument.
Similarly, you can add new attribute values or change existing ones using the setAttribute( ) method. If you want to set the href value of a specific page on url="http://easy-designs.net"/>, you could do so using setAttribute( ):
var link = document.getElementById( 'easy' ); link.setAttribute( 'href', 'http://www.easy-designs.net/index.php' );
You could also add a title to the link using setAttribute( ):
link.setAttribute( 'title', 'The Easy Designs, LLC homepage' );
This brings us to our next topic: creating document structure using the DOM.