1

How do I remove the value 'Anonymous' in this input that I have and replace it with a placeholder text 'Your name' using javascript/jquery? I don't have access to the HTML code.

This is what I have so far, but don't really know where to go from there.

document.getElementById('txtYourName').placeholder =' Your Name ';  


HTML

<input id="txtYourName" type="text" value="Anonymous" name="txtYourName"></input>
1
  • 1
    add document.getElementById('txtYourName').value = "";, the placeholder is only visible if the input has no value. Commented Mar 21, 2014 at 18:32

5 Answers 5

1

Add this second line here... A placeholder is only seen when the value happens to be blank. With the code below, you can set the placeholder, as well as erase the default value in your field.

document.getElementById('txtYourName').placeholder =' Your Name ';  
document.getElementById('txtYourName').value = "";

Demo: http://jsfiddle.net/723vL/

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

Comments

1

You can use .val():

$('#txtYourName').val('');

or pure javascript using:

document.getElementById('txtYourName').value = ""

If you want to set new value then just put your value inside "", like:

$('#txtYourName').val('new value');

With jQuery, your final code should look like:

$('#txtYourName').attr('placeholder','Your name');
$('#txtYourName').val('');

Fiddle Demo

With pure JS, you final code should look like:

document.getElementById('txtYourName').placeholder ='Your name';  
document.getElementById('txtYourName').value = "";

Fiddle Demo

2 Comments

why pure javascript is poor?
Can you explain why javascript is poor?
0

That should do it, so long as you are running that script either on an onload event, jquery's ready event or at the very bottom of the page, once the DOM has been rendered.

Are you getting an error in your console?

Comments

0

try this

returns value:

$('#txtYourName').attr("value");

sets value

$('#txtYourName').attr("value", "");

3 Comments

Actually, this is incorrect. This changes the value of the field, but does not set the placeholder text (or show the placeholder text). The OP is looking to clear out the value, not set it.
@Charlie74 : OP can always set "new_value" to "" , right?
Correct, that would be more of what the OP is asking.
-1

use

document.getElementById('txtYourName').value ='some vaue';

if you are using jQuery then you can use

$("#txtYourName").val('some value');

2 Comments

Actually, this is incorrect. This changes the value of the field, but does not set the placeholder text (or show the placeholder text). The OP is looking to clear out the value, not set it.
@Charlie74 he wants to change the value "how to remove/change an input value using javascript"

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.