4

I am basically trying to print the value of a button in the div with list class if the button is selected. Also remove the same value wwhen it is deselected. I am able to print the value successfully but not able to remove it. Could somebody please help me out with it.

var buttonSelect = $(".btn").click(function() {
    if ($(this).hasClass('active')){
        $(".list").append(this.value + "   ")
    }
    else {
        $(".list").remove(this.value)
    }
});
5
  • this is the HTML element, and $(this) is the jQuery object. Try to use $(".list").remove($(this)) Commented Mar 26, 2015 at 13:38
  • $(this) would remove the whole button aint it? It would select the object i suppose Commented Mar 26, 2015 at 13:46
  • 1
    Yes, because remove() removes an object, if you just want to clear the text you can do $(this).text("") or any of the answers listed below Commented Mar 26, 2015 at 14:23
  • jquery.remove() removes elements from the DOM, a TextNode is not an element.. however, both are "objects".. so I would refrain from using object as some sort of qualifier here.. Commented Mar 26, 2015 at 14:49
  • here.. this is the distinction.. nodeType w3schools.com/dom/dom_nodetype.asp Commented Mar 26, 2015 at 14:51

3 Answers 3

2

You should rather append the content along with html element like span:

$(".btn").click(function() {
if ($(this).hasClass('active')){
     $(".list").append('<span class="newval_'+this.value+'">'+this.value + "</span>");
}else{ 
     $(".list").find('.newval_'+this.value).remove();
}});
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much :) Worked perfect! However i dont understand the need to use the span element. Would be indebted if you could tell me the concept.
What append is doing is creating a text object and sticking it on the end. It's not a string manipulator, it's a DOM manipulator. In order to have something to remove later, you need to have an object in the DOM to remove and a text object won't work. This code adds a span object to the page as the very last thing inside .list and then removes the span object when the button is pressed again. Hope that helps!
Clear now! Thank you :)
1

The parameters from .remove() is a selector, just having a value in there that contains the content of the element you want to remove will not work. For example if I had this:

<li class="list">test</li>

Doing $(".list").remove("test") will not remove it. You will probably want to use the :contains selector (since you may have the value with spaces at the end). So in the example above doing $(".list").remove(":contains('test')") will remove the element. You can do the same thing in your case:

$(".list").remove(":contains('"+this.value+"')");

Comments

1

If you want to remove the element itself along with everything in it, just use jQuery’s remove() method.

$(document).ready(function() {
    $("button").click(function() {
        $("div").remove();  //remove Div element
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.