0

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();
            }
        }
  • http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element – Quannt Oct 21 '13 at 07:22

5 Answers5

2

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="";
Se Won Jang
  • 793
  • 1
  • 5
  • 12
2

This is invalid assignment:

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

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

Navnath Godse
  • 2,235
  • 2
  • 21
  • 31
Aryan
  • 1,467
  • 2
  • 19
  • 34
1

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

Try this:

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

Fiddle

Sergio
  • 27,160
  • 10
  • 79
  • 126
1

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>
Achilles
  • 429
  • 7
  • 26
0

try:

 document.getElementById("textmob").focus(function() {
      *do what you like with text field*;
    });
Lucky Lefty
  • 337
  • 1
  • 7