Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

jquery don't add class to specified array

I have the HTML below

<span class="post-labels">
    <a href="http://myblog.blogspot.com/search/label/jQuery%20Downloads">jQuery Donwloads</a>
    <a href="http://myblog.blogspot.com/search/label/jQuery">jQuery</a>
    <a href="http://myblog.blogspot.com/search/label/mootools">mootools</a>
    <a href="http://myblog.blogspot.com/search/label/css">css</a>
    <a href="http://myblog.blogspot.com/search/label/blogger">blogger</a>
    <a href="http://myblog.blogspot.com/search/label/wordpress">wordpress</a>
</span>​

the jquery below adds class to <a> elements ACCEPT for the url http://myblog.blogspot.com/search/label/jQuery%20Downloads http://jsfiddle.net/qbrN8/10/

$(".post-labels a").each(function(){
var domain = "http://myblog.blogspot.com/search/label/";
var dontadd = "jQuery Downloads";
var encode = encodeURI(dontadd);    
var url = domain + encode; 
var posta = $(this).attr("href");
if(posta!=url){
$(this).addClass("addedclass");
}        

});


I am trying to create an array to exclude multiple specified urls with the jquery below BUT it doesn't work http://jsfiddle.net/qbrN8/11/ .... I never have worked with arrays so i wouldn't know how to fully execute it. i have search but i keep getting result of stuff like var myarray[0] var myarray[1] where i would much rather have an array like var myarray = [];

$(".post-labels a").each(function(){
    var domain = "http://myblog.blogspot.com/search/label/";
    var dontadd = [];
    dontadd.push("jQuery Downloads", "css", "wordpress");
    var encode = encodeURI(dontadd);    
    var url = domain + encode; 
    var posta = $(this).attr("href");
    if(posta!=dontadd){
    $(this).addClass("addedclass");
    }        
});

Upvotes: 0

Views: 470

Answers (2)

dievardump
dievardump

Reputation: 2503

First : don't declare your "global" variables in your each... it makes you declare these for every elements. It is REALLY not optimized

var dontadd = ["jQuery Downloads", "css", "wordpress"], domain = "http://myblog.blogspot.com/search/label/"; $(".post-labels a").each( ...

Second : You have to check if label is in dontadd, not if label is dontadd => if (dontadd.indexOf(posta) !== -1) Working with array means that you have a collection of element, not just one. So you must check if what your looking for " is in ". indexOf returns to you the index (0 to length-1) of the element, -1 if it not exists.

Third : You never worked with array and are using jQuery ? You're doing it the wrong way. You must come back to 0, read and learn about Javascript bases, learn how to create a librarie etc... When you'll be able to make things like the one you're using, you'll be able to learn how to use jQuery.

Upvotes: 1

pimvdb
pimvdb

Reputation: 154888

!= does not work if you want to check whether an array contains a string. You can use indexOf: http://jsfiddle.net/qbrN8/13/.

$(".post-labels a").each(function(){
    var domain = "http://myblog.blogspot.com/search/label/";

    var dontadd = ["jQuery Downloads", "css", "wordpress"].map(function(v) {
        return domain + encodeURI(v);
    });
    // `dontadd` is an array of encoded, full URLs

    var posta = $(this).attr("href");

    // `!~` with `indexOf` can be used for "does not contain"
    if(!~dontadd.indexOf(posta)){
        $(this).addClass("addedclass");
    }        
});

Upvotes: 0

Related Questions