views:

231

answers:

4

Hello, When i select something from a selectbox it adds a class to the

selected option this way: $('div#tabs input[type="text"], select option:selected, input:checked, textarea').addClass('takethis'); But when i select a different option it doesn't remove the class 'takethis' from the option which I first selected. How can I force this?

+1  A: 

You need to call removeClass on the previously selected item.

Andrew Hare
What heartbreak. Beat by 30 seconds. :(
Paolo Bergantino
how would i know the previouse selected item?
sanders
Please see Paolo's answer - that is the best way to do it.
Andrew Hare
+1 for beating me :)
Paolo Bergantino
look @ my comment on paolo's answer. There is more info.
sanders
+2  A: 

You could use removeClass like this:

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

I am not sure what you are trying to do, though. This code:

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

Is grabbing all text fields, the selected option of all selects, all checked fields, and all textareas and adding the class takethis to it. Is that what you want to do?

EDIT I see what you're trying to do. If that is the case, why not just do this?

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);

Wouldn't this be simpler or am I missing a requirement here?

Paolo Bergantino
+1 For the examples, well done.
Andrew Hare
Well, I am using the ui tabs and i want to fill out a from and automaticlly fill a fourth tab to just show the filled information. So if i want to select a different option how do I make sure the previous option is not in the select?Here is the full code:http://pastebin.com/m2c0f0847
sanders
This gives weird errors: [Exception... "'type property can't be changed' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "<unknown>" data: no]
sanders
Can you try it again? I just fixed an error.
Paolo Bergantino
+3  A: 

Remove class from all non-selected options:

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

Then add to selected one:

$("select option:selected").addClass("takethis");
Seb
how could i do the same for radio buttons?
sanders
Change the selector to "input:radio" instead of "select option:selected" :)
Seb
I meant: "input:radio:not(:checked)"
Seb
Acutally it's alters the option box itself. But the contents of the option box are also changed. Rather just the values should be changed. How can I do that?
sanders
If you mean to add/remove classes to particular options, I'm afraid it's not possible to do cross-browser safe. Styles to <SELECT> boxes apply to all children.
Seb
A: 

The answer of seb works for me! Thanks all!

sanders
Please mark seb's answer as accepted if it was what you needed - thanks! :)
Andrew Hare
Thanks for your comment, Andrew :)
Seb
lol....you still have not answered his answer as accepted
TStamper