Delete class selection option not selected

When I select something from selectbox, it adds a class to

the selected option as follows:    $('div#tabs input[type="text"], select option:selected, input:checked, TextArea ') addClass (' takethis'); But when I select another option, it does not remove the 'takethis' class from the option I first selected. How can I make it?

+3
source share
3 answers

Delete a class from all parameters not selected:

$("select option:not(:selected)").removeClass("takethis");

Then add to the selected:

$("select option:selected").addClass("takethis");
+11
source

You can use removeClass as follows:

$('#tabs').find('option').removeClass('takethis');

I'm not sure what you are trying to do. This code:

$('div#tabs input[type="text"],
            select option:selected,
            input:checked,
            textarea').addClass('takethis');

, , takethis. , ?

EDIT , . , ?

var table = $('<table>');
$('div#tabs :input').each(function() {
    var val = $.trim($(this).val());
    var name = $(this).attr('name');
    if(val) {
        var td1 = $('<td>').html(name);
        var td2 = $('<td>').html(val);
        $('<input>').attr({
            type: 'hidden',
            name: name
        }).appendTo(td2);
        $('<tr>').append(td1).append(td2).appendTo(table);
    }
});
$('#4tab').append(table);

?

+2

You need to call removeClassin the previously selected item.

+1
source

Source: https://habr.com/ru/post/1705027/


All Articles