Establecer el Valor de Entrada Usando la Función Javascript


Actualmente estoy usando un gadget YUI. También tengo una función Javascript para validar la salida que viene del div que YUI dibuja para mí:

Event.on("addGadgetUrl", "click", function(){
        /* line x ------>*/     var url = Dom.get("gadget_url").value;
                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);

Aquí está mi 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>

Como puede ver mi pregunta es, ¿cómo puedo establecer el valor de gadget_url para ser ""?

Author: Nathan, 2011-04-18

6 answers

Inténtalo... para YUI

Dom.get("gadget_url").set("value","");

Con normal Javascript

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

Con jQuery

$("#gadget_url").val("");
 356
Author: Sangeet Menon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-13 13:41:24
document.getElementById('gadget_url').value = '';
 64
Author: Calum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-18 08:49:22

Lo siguiente funciona en MVC5:

document.getElementById('theID').value = 'new value';
 4
Author: Bogdan Mates,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-02 11:04:24

Dependiendo del caso de uso, marca la diferencia si usa javascript (element.value = x) o jQuery $(element).val(x);

Cuando x es undefined jQuery resulta en una cadena vacía, mientras que javascript resulta en "undefined" como una cadena.

 4
Author: thug-gamer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-19 12:24:35

No estoy usando YUI, pero en caso de que ayude a alguien más, mi problema fue que tenía ID duplicados en la página (estaba trabajando dentro de un diálogo y se olvidó de la página debajo).

Cambiar el ID para que fuera único me permitió usar los métodos enumerados en la respuesta de Sangeet.

 1
Author: BSounder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-22 23:28:07

Documento.getElementById ('gadget_url').value = 'su valor';

 0
Author: SMD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-25 07:29:45