SpeekaDievs
SpeekaDievs

Reputation: 322

js clicking adds color

I have a question. So basically i'm trying to create a js script, when user clicks on a link, it colors the link in different color and then adds the link to an input box, and when the user clicks again on the same link, it changes the color back to original and deletes the text from the input box. The purpose whould be as a search filter, where you can add to search box, predefined keywoards.

$('.tagsSelect a').click(function() {
    var clickColor = this.style.color;

    if(clickColor = "#F5EBD5"){
        var value = $(this).text();
        var input = $('#popGirlMenu');
        input.val(input.val() + value + ', ');
        this.style.color="#f5d47f";
        return false;
    }
    if(clickColor = "#f5d47f") {
        var value = $(this).text();
        var input = $('#popGirlMenu');
        input.val(input.val() - value - ', ');
        this.style.color="#F5EBD5";
        return false;
    }
});

This is my code, it works, bet when the user clicks again on the link, it doesn't change the color back to original and it doesn't remove the text from input box.

p.s sorry for my bad english

Upvotes: 1

Views: 89

Answers (2)

user4066167
user4066167

Reputation:

Use == or === (comparison) instead of = (setting a variable)

Upvotes: 2

huysentruitw
huysentruitw

Reputation: 28141

Use CSS for your layout, and check the class instead of a color value:

CSS

a { color: blue; }
a.selected { color: red; }

HTML

<a href="#">link a</a>
<a href="#">link b</a>
<input id="txt" type="text" />

JavaScript

$("a").click(function() {
    var $this = $(this);
    $this.toggleClass("selected");
    if ($this.hasClass("selected"))
        $("#txt").val($("#txt").val() + $this.text() + ", ");
    else
        $("#txt").val($("#txt").val().replace($this.text() + ", ", ""));
});

See this jsFiddle

Note to remove a part from a string, you need to replace that part with an empty string. - doesn't work on strings.

Upvotes: 4

Related Questions