JustARandomProgrammer
JustARandomProgrammer

Reputation: 277

Jquery get href link

This is my Javascript code:

$(".woocommerce-LoopProduct-link").each(function(){
        var str=$(this).attr("src");    
        console.log(str);
    });

What I want to see is a string that contain "http://mywebsitelinked.com" for example.

Now what the console display is:

a.woocommerce-LoopProduct-link
accessKey:""

And all the other thing contained in a.wooocommerce-LoopProduct-link

Upvotes: 1

Views: 19232

Answers (3)

Tomas2D
Tomas2D

Reputation: 75

If you are using link element you have to use

$(this).prop("href")` or `$(this).attr("href")

or just

this.href // (vanilla Javascript, better)

Upvotes: 2

Chandan Kumar Thakur
Chandan Kumar Thakur

Reputation: 292

try: this will match all the hrefs with mywebsitelinked.com

$(".woocommerce-LoopProduct-link").each(function(){
  var str=$(this).attr("href");
  if(str.match(/mywebsitelinked.com/gi)!=""){
    console.log(str);
  }
});

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Instead of this:

$(this).attr("src");

try

$(this).attr("href");

as you are trying to get the URL and URL is in the href attribute of the anchor.

Ex:

$(document).ready(function(){
  alert($('a').attr('href'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="http://google.com/">Google></a>

Upvotes: 4

Related Questions