dimanche 21 juin 2015

Javascript Replace function not working on first instance

I am currently trying to make a list, using javascript and jquery that returns a string of keywords as shown here: keyword+keyword+keyword+keyword

I am attempting to do this by adding a class 'active' when a keyword is clicked as shown in the code below, then checking whether it is active, if it is, add it to a list with an additional + sign (except for the first element, limited by the counter), however whenever I click on an item 2 times consecutively, it is successfully added to the list however is not deleted from it, yet the counter is added and subtracted per usual, and the console log reports the class changing. The code is below;

        $("ul.cats li, ul.colours li ").click(function(){
            $(this).toggleClass( 'active');
        });

        var taglist = '';
        var tagcounter = 1;

        $("ul.cats li.tag").click(function(){
            var tag = $(this).attr("list-data");

            if ($(this).hasClass('active')) {   

                if (tagcounter <= 0){
                    taglist += '+';
                }
                taglist += tag;
                tagcounter -= 1;
                console.log('Active');

            }  else {

                var tag2 = '+' + tag;
                taglist = taglist.replace(tag2," ");
                tagcounter += 1;
                console.log('inactive');

            }
            console.log(taglist, 'Taglist', tagcounter);

        });

From this I believe I should get a list of keyword1+keyword2+keyword3 however whenever an item is clicked twice in succession, (keyword1 is rendered active and inactive consecutively) the keyword is not deleted yet the counter is incremented and the class is rendered inactive. Using an example keyword of 'light', this is what is returned by the console;

[Log] Active 
[Log] Light Taglist 0 
[Log] inactive 
[Log] Light Taglist 1 (wordpress, line 237)

This shows that the class checks and if statement is functioning properly yet the replace function is not. Please can someone provide a solution to this, I will be eternally grateful!

Aucun commentaire:

Enregistrer un commentaire