Forum Moderators: open

Message Too Old, No Replies

Alert value of an element's attribute?

Just trying to figure out how to access existing attributes.

         

JAB Creations

5:04 am on Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can I alert an element's attribute? In example below I'd like to alert "Red Shoes".

- John

alert(document.getElementById('example').attr.title);

<element id="example" title="Red Shoes" />

Edited for minor mistake.

[edited by: JAB_Creations at 5:05 am (utc) on Sep. 20, 2008]

Fotiman

5:11 am on Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



alert(document.getElementById('example').title);

In other words, the attribute can be accessed with the dot notation, as in object.property. Alternatively, you could use array notation:

alert(document.getElementById('example')['title']);

JAB Creations

5:37 am on Sep 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's like planning to look for buried treasure on another island when you're standing three feet above it with a shovel in my hands; thanks!

- John