tags:

views:

47

answers:

5

I need to remove some values from a hidden and text input box using JQuery, but somehow this is not working

Example:

<input type="hidden" value="abc" name="ht1" id="ht1" />
<input type="text" name="t1" id="t1" />

I use the following JQuery code to remove the values with an onclick event

$('#rt1').click(function() {
    $('#t1').val();  
    $('#ht1').val();  
});

I can I empty the contents of the input box and clear the value of the hidden field using JQuery?

+3  A: 
$('#rt1').click(function() {
    $('#t1').attr('value', '');  
    $('#ht1').attr('value', '');  
});
Mark B
+4  A: 

You should do this:

$('#rt1').click(function() {
    $('#t1').val('');  
    $('#ht1').val('');  
});

That is, pass an empty string. Either that, or use removeAttr (query.removeAttr('value')).

DrJokepu
+1 for `removeAttr()`
Russ Cam
+1, Seconded, I would say removeAttr() is the preferred way to do this.
karim79
+4  A: 

You just need to pass an empty string in the val() function or, you can use the more generic attr() function that sets a given attribute to a given value:

$('#rt1').click(function() {
    $('#t1').attr("value", "");  
    $('#ht1').attr("value", "");  
});
rogeriopvl
+1  A: 

This should be:

$('#rt1').click(function() {
    $('#t1').val('');  
    $('#ht1').val('');  
});

When val() function doesn't have parameter it will serves as getter not setter

jerjer
+1  A: 

Shorter version

$('#rt1').click(function() {
    $('#t1, #ht1').val('');  
});
dotty