ashish kumar
ashish kumar

Reputation: 5

JavaScript function giving Uncaught reference error while getting the value of text box

I am trying make textbox blank and set focus on it using Java Script.
I used following code for that

document.getElementById("textmob")="";
document.getElementById("textmob").focus;

But I get following error:

Uncaught ReferenceError: Invalid leftend side assignment;

My Html Code

<asp:TextBox ID="mob" runat="server" CssClass="text" AutoPostBack="True" onchange = "validateMob(this)" ></asp:TextBox> 

My validation function

 function validateMob(txtmob)

          {
            debugger;
            var rx = /^\d{10}$/;

           if (!txtmob.value.match(rx)) {
               alert('Invalid Mobile No');
               // document.getElementById("mob") = "";
              document.getElementById("mob").focus();
            }
        }

Upvotes: 0

Views: 1212

Answers (5)

Achilles
Achilles

Reputation: 519

Its just a minor mistake in your syntax it should be like that

document.getElementById("textmob").value="";
document.getElementById("textmob").focus();

check complete example

<html>
<head>
<script>
function my()
{
        document.getElementById("textmob").value="";
        document.getElementById("textmob").focus();
}
</script>
</head>
<body>

<input id="k" type="text">
<input id="textmob" type="text">

<button onclick="my()" id="b" value="Click ME">


</body>
</html>

Upvotes: 1

Aryan
Aryan

Reputation: 1877

This is invalid assignment:

document.getElementById("textmob")="";

You are trying to assign a string object to a JavaScript element. I guess .value is missing.

Upvotes: 2

Sergio
Sergio

Reputation: 28837

I think you forgot to use .value and () in foucus()

Try this:

document.getElementById("textmob").value = "";
document.getElementById("textmob").focus();

Fiddle

Upvotes: 1

Lucky Lefty
Lucky Lefty

Reputation: 347

try:

 document.getElementById("textmob").focus(function() {
      *do what you like with text field*;
    });

Upvotes: 0

Se Won Jang
Se Won Jang

Reputation: 783

You can't just set it to "".

document.getElementById('textmob')

gives you the element itself.

In order to set the content of the element to "",

use .value.

document.getElementById("textmob").value="";

Upvotes: 2

Related Questions