Link to home
Start Free TrialLog in
Avatar of JFairbanks
JFairbanksFlag for United States of America

asked on

How do I obtain an anchor's attribute value via javascript

Ok, so I've got an ASP.NET TreeView Control which I have all wired up to catch a client-side click event.  Now I want to figure out the Value of the clicked node.

My nodes are rendering as (simplified):
<td>
        <input type="checkbox" name="myCheckBox" id="myCheckBox" />
        <a href="javascript:__doPostBack('someName','s283\\289')>
                The Node Name
        </a>
</td>

I've obtained a reference to the specific node via:
function TreeViewOnClick() {
            var item = window.event.srcElement;
            if(item.tagName == 'INPUT' && item.type == 'checkbox') {
                    // a checkbox has been clicked
                    // now get the value from the corresponding anchor tag
                    // ...Here...
            }
}

What I want is the "289" from the href in the <a> of the <td>.  I understand I'll have to use a substring to get the specific "289" I want, however how do I get a reference to the corresponding anchor from the input reference I already have from 'window.event.srcElement;'?
ASKER CERTIFIED SOLUTION
Avatar of alien109
alien109
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michel Plungjan
I suggest (since href="javascript:..." will render the browser unstable

this:

      <a href="#" id="'someName" title="s283\\289" onClick="__doPostBack(this.id,this.title)" >
                The Node Name
        </a>

then you can do
var title = theCheck.parentNode.getElementsByTagName('a')[0].title

Avatar of JFairbanks

ASKER

That worked perfectly.  I knew it was simple, just couldn't figure it out.