Solved

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

Posted on 2009-05-11
4
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

688 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