March 24, 2012

Hide the span with a class name jquery

Question by Sethen Maleno

I am stuck on what to do when hiding a span that has a certain class name. I can’t use this because it refers to the input. Here is my script:

//uncheck all checkboxes
$("input[type=checkbox]").prop("checked", false);

$("input[type=checkbox]").each( function (index) {
    $(this).addClass("doc" + index);
})

$("input").change( function () {

    var docName = $(this).parent().find("span");
    var className = $(this).attr("class");

if(this.checked) {

        $("span.noneAttached").fadeOut('slow', function () {

            docName.clone().appendTo(".attachedDocuments").addClass(className).after("<br />").text();

        });
    }

else if (!this.checked && ($(".attachedDocuments > span").hasClass(className))) {


    //hide the span with the class name

}

});

The else if checks to see if a checkbox is not checked and if the parent div contains any children with the class name. If so, hide it.

Where do I go from here? I am sure this answer is obvious, but I am just not seeing it.

Answer by Starx

Concatenate the class name to the selector like this

$("span."+className).hide();

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!