0

I have a function CheckMobile(MobNumber) and it will check all duplicate record of MobNumber which is passed in function. I want to use jquery to implement this.

<td><input type='text' name='Phone2' class='required' maxlength='10' /></td>  
<td><a href="javascript:void(0)" onclick="CheckMobile(Phone2)">Check Phone</a></td>  

and my jquery function is:

function CheckMobile(MobNumber) {
    debugger;
   var phone = $(MobNumber).val();
   alert(phone);

}

But it does not work. In function i am getting undefined value.Please help how to get value of phone.

1
  • What is Phone2 in the onclick? Is it a variable or did you mean it to be a selector for the input? Commented Mar 31, 2012 at 19:29

3 Answers 3

1

You can get at the input by doing:

$("input[name='Phone2']")

In jQuery and then use .val() to get the value. (Working JSFiddle: http://jsfiddle.net/PhT94/)

EDIT:

Didn't notice you'd given it an ID, in that case you could just do:

$("#Phone2").val()

So in your original code change:

$(MobNumber).val()

To

$("#" + MobNumber).val()
Sign up to request clarification or add additional context in comments.

Comments

0

You need to change how you use the selector.

<td><input type='text' name='Phone2' id='Phone2' class='required' maxlength='10' /></td>  
    <td><a href="javascript:void(0)" onclick="CheckMobile('Phone2')">Check Phone</a></td> 

Not that we append '#' and concatenate with the passed string parameter

 function CheckMobile(MobNumber) {
        debugger;
       var phone = $('#'+MobNumber).val();
       alert(phone);

    }

Comments

0

the problem is in your selector there, it's not valid :

      var phone = $(MobNumber).val();

You should access by the attribute name instead like this

  var phone = $("input[name='Phone2']").val();

2 Comments

This is wrong, the issue is that $(MobNumber) goes to $('Phone2) which is not a valid selector.
I don't see in what my code is wrong at all if you replace his lin by my is working. And my answer is that the selector is wrong...

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.