set value of input element with jquery

6.7k Views Asked by At

I am trying to change the value of the inputfields with JQuery and I have no idea why this doesn't work. It does not throw an error but it just doesn't change. I'm building with appgyver steroids but i don't think that matters.

javascript:

$(document.getElementById("url")).val('test');

html

 <input type="text" id="url" class="topcoat-text-input"
       style="margin-left:10%; margin-top:10px; width: 80%; text-align: center" autocapitalize="off"
       autocorrect="off" required="true"
       placeholder="something else"/>
1

There are 1 best solutions below

2
On

Make sure that your code is running after the DOM is loaded.

Also there is no need for jQuery in your example.

You can just do:

window.onload = function(){
    document.getElementById("url").value = 'test';
};

Demo: http://jsfiddle.net/qwertynl/7uCfc/


And if you want to do full on jQuery:

$(function(){
    $('#url').val('test');
});

Demo: http://jsfiddle.net/qwertynl/m5Ttn/


Or you could not wrap your code in an onload and just put the whole script at the end of the document after the page has loaded already.