Solved

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

Posted on 2009-05-11
4
241 Views
Last Modified: 2012-05-06
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;'?
0
Comment
Question by:JFairbanks
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 125 total points
ID: 24358808
Try using the element's nextSibling property

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...
                    var hrefProp = item.nextSibling.getAttribute("href");
            }
}

Keep in mind that you may end up getting returned a text node and will need to check for this.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24362298
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

0
 

Author Closing Comment

by:JFairbanks
ID: 31580288
That worked perfectly.  I knew it was simple, just couldn't figure it out.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24365536
Hmm
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question