David Van Staden
David Van Staden

Reputation: 1779

Check if div contains anchor with specific href value

Good Day

I want to check if there is a specific link inside my page that contains the following url:

http://mysite.com/category/products

I am using the following, but I don't think it is correct (since it does not work)

if $('#breadcrumbs a.category').attr('href') == "http://mysite.com/category/products";
    alert('It is here!');

The HTML Structure looks like this:

<div id="breadcrumbs">
<a class="category" href="">generated by php</a>
<a class="category" href="">generated by php</a>
<a class="category">generated by php</a>
</div>

The top link will be somewhere in this hierarchy...

thank you!

Update: page

Upvotes: 1

Views: 4178

Answers (5)

PSR
PSR

Reputation: 40318

$('#breadcrumbs').click(function(){

 $(this).children('a').each(function(){
    if($(this).attr('href')=="your url")
        .....
 });
});

Upvotes: 0

Oliver
Oliver

Reputation: 9002

You can use the attribute selector:

if ($('#breadcrumbs .category[href="http://mysite.com/category/products"]').length > 0) {
    alert("It's here");
}

Documentation:

http://api.jquery.com/attribute-equals-selector/

Upvotes: 4

Ram
Ram

Reputation: 144659

attr method only returns the value of the first selected element, you can use filter method or attr selector.

$(function() {
    var len = $('#breadcrumbs a.category').filter(function(){
         return this.href === 'http://mysite.com/category/products';
    }).length;

    if (len) {
        alert('It is here!');
    }
});

Upvotes: 1

Jai
Jai

Reputation: 74738

Try shorter:

($('[href="http://mysite.com/category/products"]').length > 0) ? alert('Yes there are') : alert('oops');

Tryout in this fiddle

Upvotes: 1

Devang Rathod
Devang Rathod

Reputation: 6736

$(document).ready(function() {
   if($('#breadcrumbs a.category').attr('href') == "http://mysite.com/category/products")
   {
       alert('It is here!');
   }    
});  

Upvotes: 0

Related Questions