0

I am trying to add a hidden input field to a form when a span with class="checkbox" is clicked, and remove that same field when span with checkmark is clicked.

I am able to add the input, but I cannot remove it.

My HTML:

<span title="Here's My Title" class="wishlistcheck checkbox">1</span>

<form id="wishlistform"></form>

My Javascript:

$(document).ready(function() {

    var appNum = 0;

    $("span.wishlistcheck").click(function() {
        var el = $(this);
        var appName = el.attr("title");

        var isChecked = el.hasClass('checkmark');
        if(!isChecked) {
            el.removeClass('checkbox');
            el.addClass('checkmark');

            appNum++

            $("#wishlistform").append("<input id=\"" + appName + "\" type=\"hidden\" name=\"product_" + appNum + "\" value=\"" + appName + "\">");


        } else {
            el.removeClass('checkmark');
            el.addClass('checkbox');

            appNum--

            $("input#" + appName).remove();




        }
    });
});
1
  • 2
    you are aware that appNum++ and appNum-- are statements, and need a semicolon after them right? Commented May 7, 2012 at 23:05

2 Answers 2

2

I guess you have to put the appNum-- after the .remove() method calling. Try it:

$(document).ready(function() {
    var appNum = 0;
    $("span.wishlistcheck").click(function() {
        var el = $(this);
        var appName = el.attr("title");
        var isChecked = el.hasClass('checkmark');
        if(!isChecked) {
            el.removeClass('checkbox');
            el.addClass('checkmark');
            appNum++
            $("#wishlistform").append("<input class=\"" + appName + "\" type=\"hidden\" name=\"product_" + appNum + "\" value=\"" + appName + "\">");
        } else {
            el.removeClass('checkmark');
            el.addClass('checkbox');
            appNum--
            $("input[class=" + appName + "]").remove();
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

So try using the same code, but selecting it by it's class (as rposky said), an not by it's id
Perfect! It works like this too... $("input[id=" + appName + "]").remove();
2

You probably have multiple DOM elements by that id tag, and so it may be removing an element matching that id but not the right one.

Try giving it a class name instead and getting more specific in your jQuery parameters by adding parent elements, i.e.

 $('#wishlistform input#' + appName).remove();

3 Comments

You say to give it a class name instead, and yet you still use # instead of . in the selector ?
I tried that too (using the parent form id, even though it is the only element on the page with that id). It didn't work.
True, I didn't know what class name he would assign though :p

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.