JavaScript Get HREF value by class name

I'm trying to get href value of a specific anchor tag placed on a html page.

html tag structure

<a rel="nofollow" title="some title" target='_blank' href="http://www.this-is-what-i-need.com/just-this.html"><span><em class="buttonGo"></em>Go to this page</span></a>

How do I get the href value from above anchor tag using buttonGo class name?

Answer:1

A single line of plain Javascript will give you what you need:

var href = document.getElementsByClassName("buttonGo")[0].parentNode.parentNode.href;

But, if you aren’t sure how many times you need to use parentNode, you have to use a loop:

var anchor = document.getElementsByClassName("buttonGo")[0];
do{
anchor = anchor.parentNode;
} while(anchor.nodeName.toLowerCase() != "a");
var href = anchor.href;

Demo: Solution One (created by Milche Patern) and Solution Two

Answer:2

Using jQuery:

$('.buttonGo').closest('a').attr('href');

Without jQuery, you'll need to start with document.getElementsByClassName('buttonGo'), then crawl up parentElement until you find an a.

jQuery demo

Answer:3

I've added a click event to a parent #carousel-thumbs. carouselThumbsContainer.onclick = function(ev) { var target = ev.target; // which child was actually clicked } <ul id="carousel-...

I've added a click event to a parent #carousel-thumbs. carouselThumbsContainer.onclick = function(ev) { var target = ev.target; // which child was actually clicked } <ul id="carousel-...

  1. javascript index child
  2. javascript child index in parent
  3. javascript index of child element
  4. javascript index of child node
  5. javascript get child index
  6. javascript getchild index in parent
  7. javascript find child index
  8. javascript dom child index
  9. javascript set child index
  10. javascript get index of child element
  11. javascript get index of child node
  12. javascript dom get child index
  13. javascript find index of child element

Let's say we have a ball that can be moved left and right around the screen. When you click space, the ball should jump. I got the ball to move left and right in a canvas. However, when the ball is ...

Let's say we have a ball that can be moved left and right around the screen. When you click space, the ball should jump. I got the ball to move left and right in a canvas. However, when the ball is ...

I am unable to append values into the _summary class or any other. I believe its a really simple syntax error I am making. I have been trying different syntax's for the past day, with no luck. I am ...

I am unable to append values into the _summary class or any other. I believe its a really simple syntax error I am making. I have been trying different syntax's for the past day, with no luck. I am ...

  1. append event jquery
  2. append event
  3. append event listener
  4. append event javascript
  5. append event listener jquery
  6. append event handler jquery
  7. append event onclick
  8. append event handler c#

Here is my d3.js code , from here on right click i am calling the function contextmenu // Display the tiles for each bucket. var tile = svg.selectAll(".tile") .data(buckets) .enter().append("...

Here is my d3.js code , from here on right click i am calling the function contextmenu // Display the tiles for each bucket. var tile = svg.selectAll(".tile") .data(buckets) .enter().append("...

  1. right click opens link in new tab
  2. right click opens windows installer
  3. right click opens new window
  4. right click opens on left
  5. right click opens properties
  6. right click opens link
  7. right click opens window
  8. right click menu opens on left side
  9. right click automatically opens
  10. autocad right click opens help
  11. firefox right click opens link
  12. shift right click opens powershell
  13. double right click opens properties
  14. right click menu opens slow
  15. chrome right click opens new tab
  16. right click mouse opens
  17. double click opens right click menu