285

I'm currently using a YUI gadget. I also do have a Javascript function to validate the output that comes from the div that YUI draws for me:

Event.on("addGadgetUrl", "click", function(){
    var url = Dom.get("gadget_url").value;  /* line x ------>*/
    if (url == "") {
        error.innerHTML = "<p> error" /></p>";
    } else {
        /* line y ---> */ 
        /*  I need to add some code in here to set the value of "gadget_url" by "" */
    }
}, null, true);

Here is my div:

<div>
<p>URL</p>
<input type="text" name="gadget_url" id="gadget_url" style="width: 350px;" class="input"/>
<input type="button" id="addGadgetUrl" value="add gadget"/>
<br>
<span id="error"></span>
</div>

As you can see my question is, how can I set the value of gadget_url to be ""?

1
  • 1
    Try...Dom.get("gadget_url").set("value","");...I'm not quite sure though Commented Apr 18, 2011 at 8:50

7 Answers 7

608

Javascript

document.getElementById('gadget_url').value = '';

jQuery

$("#gadget_url").val("");

YUI

Dom.get("gadget_url").set("value","");
Sign up to request clarification or add additional context in comments.

1 Comment

goodForm.coolinput.value='foo', is not possible, why cant I use it instead of addressing the input directly by id`? Wait, here..
87
document.getElementById('gadget_url').value = '';

1 Comment

@Alastair I confirm - that is not using YUI. However OP in title write "Set Value of Input Using Javascript Function" (not YUI function) so answer meet question requirements
7

The following works in MVC5:

document.getElementById('theID').value = 'new value';

2 Comments

javascript != MVC5
And yet he gave the correct answer. There are newbies that will find this question based on this answer.
4

Depending on the usecase it makes a difference whether you use javascript (element.value = x) or jQuery $(element).val(x);

When x is undefined jQuery results in an empty String whereas javascript results in "undefined" as a String.

Comments

1

I'm not using YUI, but my issue was that I had duplicate ID's on the page (was working inside a dialog and forgot about the page underneath).

Changing the ID so it was unique allowed me to use the methods listed in Sangeet's answer.

1 Comment

Just for brevity and future reference, all id's on the page should ALWAYS be unique
0
arguments[0] = Element.prototype.querySelector.call(document.body, "#gadget_url")
HTMLElement.prototype.removeAttribute.call(arguments[0], "value")

or:

arguments[1] = Object.prototype.__lookupSetter__.call(HTMLInputElement.prototype, "value")
arguments[1].call(arguments[0], String())

note that when changing the "value" attribute's value through javascript, chrome browsers may behave very erratically if using the back button to return to the page (even if the cache is cleared, or you are testing a page on your hard disk, i.e., the page is indeed reloaded!). The new input value attribute will indeed be set to a new value (you can see the new value in F12 Developer Tools or can query it programmatically thru javascript), but the browser will still visually display the old value. It seems to happen if you set the value by using "value = ''" or by using the "_lookupSetter_", which is the same thing, or if you are creating the input on the fly with a "value" attribute present and a value assigned to it:

SomeParentNode.innerHTML = '<input class="jump-input" type="text" value="' + Math.random() + '">'

Use removeAttribute / setAttribute instead. however, this won't help if you type something into the input field manually, then return to the page by using the back button, 'cause the browser prefills the value you typed before (even if cache cleared or using a local page on your hard disk), so you need to wait until it does that before removing the attribute:

setTimeout.call(self, function()
{
    //Element.prototype.setAttribute.call(arguments[0], "value", "")

    arguments[0].value = ""

}, 1, arguments[0])

Comments

-6

Shortest

gadget_url.value=''

addGadgetUrl.addEventListener('click', () => {
   gadget_url.value = '';
});
<div>
  <p>URL</p>
  <input type="text" name="gadget_url" id="gadget_url" style="width: 350px;" class="input" value="some value" />
  <input type="button" id="addGadgetUrl" value="add gadget" />
  <br>
  <span id="error"></span>
</div>

This is the shortest working solution (JSFiddle).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.