Solved

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

Posted on 2009-05-11
4
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
That worked perfectly.  I knew it was simple, just couldn't figure it out.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Hmm
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

762 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now