Reputation: 6355
Hi I have a number of inputs for telephone numbers, they use the same class and the same show/hide techinque. I want to be able to clear the contents of the phone number in the input box, i.e. with the class name of input_tel.
However it seems to clear all inputs which I assume is because I am using the following line; input:text
, when I put in a class it just fails to work.
My JS and some of my html is below or view a jsFiddle:
$(".contact_numbers").on('click', '.clearnumber', function () {
$(this).siblings('input:text').val('');
});
<div class="telephonetwo contact_numbers">
<input type="text" class="smallinput contactpagelabel" name="faxname" size="10" value="Fax">
<input type="checkbox" class="contact_no" name="showfax" value="Y">
<input type="text" name="fax" size="30" value="016128 13456" class="input_tel">
<a href="#" class="remove">Hide</a> <a href="#" class="clearnumber">Clear #</a>
</div>
I want to be able to clear the closest input_tel rather than all of them, as their are multiple numbers.
Thanks in advance
Upvotes: 1
Views: 18246
Reputation: 304
The safe way to clear input fields with jquery
$.fn.noText = function() {/*use the jquery prototype technology to make a chainable clear field method*/
if(this.is('input')){ /*check to c if you the element type is an input*/
this.val('');/*clear the input field*/
}return this;/*means it is chainable*/
};
$('input').noText();
Upvotes: 0
Reputation: 3083
replace:
$(this).siblings('input:text').val('');
with
$(this).siblings('input:text').closest('.input_tel').val('');
Upvotes: 3
Reputation: 13135
How about targeting the input_tel
class then?
$(".contact_numbers").on('click', '.clearnumber', function () {
$(this).parent().parent().find('input.input_tel').val('');
});
Assuming no other input fields have that input_tel
class on them.
Upvotes: 2
Reputation: 294
This should do it...
$(".contact_numbers").on('click', function () {
$(".input_tel").val('');
});
Upvotes: 1