Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .val() not working when setting a variable

If i have an input like so:

<input type="text" id="textvalue" />

the following code will change its value:

$(document).ready(function() {
    $('#textvalue').val("hello");
});

however the following will not work:

$(document).ready(function() {
    var = "hello";
    $('#textvalue').val(var);
});

Why does the second one not work? I need to be able to change the value of the textbox to the value of a variable

like image 495
geoffs3310 Avatar asked Dec 03 '22 02:12

geoffs3310


1 Answers

Your var statement needs to look something like this

var something = "hello"

$('#textvalue').val(something );

Right now your not actually assigning a value to a variable, and then you are trying to use the var keyword.

Variable Reference

like image 51
Loktar Avatar answered Dec 15 '22 10:12

Loktar