tags:

views:

68

answers:

2
function areaMe(area){
        var barea = $('#barea').val();
        if(barea.indexOf(area)!=-1){
         barea=barea.replace(area, ""); // remove
        }else{
         barea+=area; // inlcui
        }
        $('#barea').val(barea);
       }

It doesn't work if the #barea input is hidden...

+2  A: 

For a hidden input I always use

var barea = $('#barea').attr('value');

$('#barea').attr('value',barea);

not val(). I've never had luck changing the value of an input type=hidden any other way.

If barea isn't a input type=hidden, then you'll need to include the html for it. Note that it must be some type of input as val() only works on inputs, selects, and textareas.

tvanfosson
Right. I've changed the input to 'hidden' and removed the 'display:none' and it works fine.
Paulo Bueno
That's strange. I have code where I've been able to test and change values of hidden inputs, no issue.
CAbbott
@CAbbott Me, too. Go figure...
Sonny Boy
A: 

You need to provide more info. I've always been able to use val() on hidden inputs (in the type="hidden" sense, although I tested with "disply:none" ones too and they also worked). I even tested one just now, and sure enough it worked with both the "get" and "set" forms of val(). So while .attr("value") might work, it really shouldn't be needed. Can you please provide:

A) Which of the two forms is failing for you (the "get" or the "set" form)?

B) The HTML which defines your hidden input?

machineghost