[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-05-11
4
Medium Priority
?
262 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 500 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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

649 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