How To Get The Href Value Of An Anchor Tag With Javascript From A String
How do I get the href value of last anchor tag in the string content below, with pure javascript, without Jquery. var contents = '
Solution 2:
Generate a temporary element(or use DOMParser) with the html content and get the attribute of a tag within it.
var contents = '<div id="content"><a href="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg"><img title="Ruffcoin-Made-In-Aba.jpg" class="alignnone size-full" alt="image" src="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg" /></a>↵↵↵↵Indigenous heavyweight, <a href="http://www.okhype.com/tag/ruffcoin">Ruffcoin</a> recently dropped a new album titled “Made In Aba“. I’ve been giving it a few spins, and I must say, the album is quite solid.↵↵[matrix]↵↵Two of my early faves are “Aba Made” and “Higher Higher which feature <a href="http://www.okhype.com/tag/sparkle">Sparkle</a>)”. You should check ’em out, then download on iTunes <a href="https://itunes.apple.com/us/album/made-in-aba/id1180901826?ign-mpt=uo%3D4"><strong>hereq</strong></a>.↵↵Cop below.↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3">DOWNLOAD Ruffcoin - Aba Made</a></strong>↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3">DOWNLOAD Ruffcoin ft Sparkle - Higher Higher</a></strong></div>';
var temp = document.createElement('div');
temp.innerHTML = contents;
// if you want to get a tag within strong tagconsole.log(
temp.querySelector('strong a').getAttribute('href')
);
// if you want to get the last linkvar ele = temp.getElementsByTagName('a');
console.log(
ele[ele.length - 1].getAttribute('href')
);
Solution 3:
var contents = '<div id="content"><a href="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg"><img title="Ruffcoin-Made-In-Aba.jpg" class="alignnone size-full" alt="image" src="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg" /></a>↵↵↵↵Indigenous heavyweight, <a href="http://www.okhype.com/tag/ruffcoin">Ruffcoin</a> recently dropped a new album titled “Made In Aba“. I’ve been giving it a few spins, and I must say, the album is quite solid.↵↵[matrix]↵↵Two of my early faves are “Aba Made” and “Higher Higher which feature <a href="http://www.okhype.com/tag/sparkle">Sparkle</a>)”. You should check ’em out, then download on iTunes <a href="https://itunes.apple.com/us/album/made-in-aba/id1180901826?ign-mpt=uo%3D4"><strong>hereq</strong></a>.↵↵Cop below.↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3">DOWNLOAD Ruffcoin - Aba Made</a></strong>↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3">DOWNLOAD Ruffcoin ft Sparkle - Higher Higher</a></strong></div>';
// Create element that will temporarially house the elements in the stringvar temp = document.createElement('div');
// Convert the string to child elements
temp.innerHTML = contents;
// Get all the linksvar links = temp.querySelectorAll("a");
// Get the contents of the href attribute of the last link:console.log(links[links.length-1].href);
Solution 4:
Try This :
var links = snippet.getElementsByTagName("a");
links(links.length - 1).getAttribute('href');
Solution 5:
Try this:
var contents = '<div id="content"><a href="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg"><img title="Ruffcoin-Made-In-Aba.jpg" class="alignnone size-full" alt="image" src="http://www.okhype.com/wp-content/uploads/2016/12/ruffcoin-made-in-aba.jpg" /></a>↵↵↵↵Indigenous heavyweight, <a href="http://www.okhype.com/tag/ruffcoin">Ruffcoin</a> recently dropped a new album titled “Made In Aba“. I’ve been giving it a few spins, and I must say, the album is quite solid.↵↵[matrix]↵↵Two of my early faves are “Aba Made” and “Higher Higher which feature <a href="http://www.okhype.com/tag/sparkle">Sparkle</a>)”. You should check ’em out, then download on iTunes <a href="https://itunes.apple.com/us/album/made-in-aba/id1180901826?ign-mpt=uo%3D4"><strong>hereq</strong></a>.↵↵Cop below.↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_-_Aba_Made_Okhype.com_.mp3">DOWNLOAD Ruffcoin - Aba Made</a></strong>↵↵[audio mp3="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3"][/audio]<strong><a href="http://www.okhype.com/wp-content/uploads/2016/12/Ruffcoin_ft_Sparkle_-_Higher_Higher_Okhype.com_.mp3">DOWNLOAD Ruffcoin ft Sparkle - Higher Higher</a></strong></div>';
var hrefs = contents.match(/href="([^"]*)/g);
var href = hrefs[hrefs.length-1];
console.log(href);
Post a Comment for "How To Get The Href Value Of An Anchor Tag With Javascript From A String"