0

I have a simple input html field. The value of this input field should be used in a jquery function later on. How can I accomplish this? I think I have to either pass it directly to the function or store it - at least - temporary. Is there a way other than have it passed to my mySQL database and retrieve it?

Here's what I tried:

html:

Firstname: <input type="text" id="firstname" name="firstname">

js:

var probe1 = $('#firstname').val();

... and then I thought about using the variable probe1 in my js script - Does not work.

The script I want to use the variable in is in another script tag.

ben_aaron
  • 1,504
  • 2
  • 19
  • 39
  • if you remove var and make it probe1 = $('#firstname').val(); then probe1 will be global. However, global variables are bad and actually, $('#firstname').val() should work in any javascript file on the same page assuming they are in the same frame(not in a different iframe) – AmmarCSE Apr 19 '14 at 18:49
  • 1
    Take a look at the variable scope of javascript: http://stackoverflow.com/questions/500431/javascript-variable-scope It might help clear things up. – Rick Lancee Apr 19 '14 at 18:49
  • 1
    We need to understand the scope of this variable. A good solution would be to save the value to a cookie or localStorage and refer to it in your function. – Mister Epic Apr 19 '14 at 18:50
  • Thank you @ChrisHardie, I dived into localStorage and it did the trick. – ben_aaron Apr 21 '14 at 17:42

1 Answers1

1

var probe1 is probably just out of scope in your second script tag.

Simply redeclare the variable in the new script, or just use $('#firstname').val() in place of probe1 in the second script.

themerlinproject
  • 3,542
  • 5
  • 37
  • 55