kritya
kritya

Reputation: 3362

javascript variables containg jquery api evaluated at calling or assignment time?

If i assign a variable something like this

var a = $("input").val();

At the start of the program. And then call something like this :

$("div").html(a);

Will this print the value of the value which was the value in the input when the var a = $("input").val(); was executed or when the line : $("div").html(a); was executed ?

Upvotes: 0

Views: 56

Answers (3)

js1568
js1568

Reputation: 7032

The first line (var a = $("input").val();) sets the value of a variable to whatever data is available. Later on when you use that variable (a), it will be the same value. It is only a string, it is not the actual DOM element.

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

a will contain the value when you first assigned it so the that value will be set in the div html

Upvotes: 0

bcoughlan
bcoughlan

Reputation: 26627

It will print the value that it had when you first assigned it.

Edit: If you want to work with its current value, just evaluate it each time:

$("div").html( $("input").val() );

Upvotes: 2

Related Questions