cyberfly
cyberfly

Reputation: 5858

Select all hyperlink with id that is visible

I have lot of this hyperlink in my page. Some of them are visible and some are hidden.

 <a class="close-reveal-modal" id="close_car2">&#215;</a>
 <a class="close-reveal-modal" id="close_car3">&#215;</a>
 <a class="close-reveal-modal" id="close_car4" style="display:none;" >&#215;</a>

So right now i want to iterate through all the visible hyperlink that the id start with "close_" and get the index, assign unique text to the hyperlink.

This is sample of my code but it is not working.

$("a[id^='close_']").is(':visible').each(function (index) {
        if(index=='0')
        {
            $(this).text('test');
        }
    }); 

How to solve this? Thanks

Upvotes: 3

Views: 459

Answers (2)

Jason Gennaro
Jason Gennaro

Reputation: 34855

Something like this will work

$('a[id*="close_"]').each(function(i){
    if($(this).is(':visible')){
        if(i == 0){
            $(this).text('Link One');
        }
        if(i == 1){
             $(this).text('Link Two');
        }        
    }    
});

Example: http://jsfiddle.net/xC4Fp/1

Upvotes: 1

sberry
sberry

Reputation: 132018

Try:

$("a[id^='close_']:visible").each(function (index) {

From the docs on "is()":

Unlike other filtering methods, .is() does not create a new jQuery object. Instead, it allows you to test the contents of a jQuery object without modification.

Upvotes: 2

Related Questions