1

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>

If you are viewing my JSfiddle click the reveal additional numbers for the clear button to appear.

Update

I want to be able to clear the closest input_tel rather than all of them, as their are multiple numbers.

Thanks in advance

4 Answers 4

3

replace:

$(this).siblings('input:text').val('');  

with

$(this).siblings('input:text').closest('.input_tel').val('');  

JSFIDDLE DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

that clears the whole line I want it to clear the closest input_tel class
not to worry changed your type to class and replaced the name
2

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.

1 Comment

What if you step up two levels with parent() and then target the class, like my new edit?
1

This should do it...

$(".contact_numbers").on('click', function () {
  $(".input_tel").val('');
});

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.