gopi nathan gopi nathan - 6 months ago 12
SQL Question

how to get the value when onclick and again on click value should be removed in jquery?

I have a page where an anchor tag has id. When the anchor tag is clicked I am fetching the value to input type hidden and after again clicking on the same div it should be removed. The selection will be in a loop. Please check my code:

$(document).ready(function() {
$(".get_category").on('click', function(){
var cat_id = $(this).attr('id');
$("#categories"+cat_id).val(cat_id);
})

});


https://jsfiddle.net/0zu17bn4/

Answer

you need to add these lines :

$(".get_category").on('click', function(){  
        var cat_id = $(this).attr('id');
        if ($("#categories"+cat_id).val() == '')
            $("#categories"+cat_id).val(cat_id);
        else
            $("#categories"+cat_id).val('');
})