Link to home
Create AccountLog in
Avatar of adman08
adman08

asked on

How to pass a JavaScript variable to an HTML textbox default value?

Hi Experts,

I'm creating some locally running, clickable flows using HTML and JavaScript.  In one step I'm retrieving values from an external system and storing them in a cookie for use later in the flow.  In a later HTML page I read the 2 values from the cookie and store them in 2 JavaScript variables, myvalue1 and myvalue2.  This happens through some cookie functions in JavaScript during an HTML onload.

This page has 2 HTML textboxes as follows:
<input type="text" name="tb1" size="25" id="id1" value=defaultvalue1>
<input type="text" name="tb2" size="25" id="id2" value=defaultvalue2>

How can I use the JavaScript variables myvalue1 and myvalue2 so they appear as the default textbox values defaultvalue1 and defaultvalue2?
Avatar of Kiran Paul VJ
Kiran Paul VJ
Flag of India image


document.getElementById('id1').value = your_variable_name;
SOLUTION
Avatar of Kiran Paul VJ
Kiran Paul VJ
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of adman08
adman08

ASKER

Thanks for responding.  So these are added as a JavaScript function (or to an existing function) and the HTML textboxes remain the same as before?
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of adman08

ASKER

I used the first method and verified the variables are global by displaying them just prior to setting the form values.
      alert(defssid+"|"+defwepkey);
      document.getElementById('one').value = defssid;
      document.getElementById('two').value = defwepkey;
                     alert("finished");
I get my first alert and the values are correct.  However, I'm not seeing the 'finished' alert.  What would cause this?
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>>However, I'm not seeing the 'finished' alert.  What would cause this?

try this

if(document.getElementById('two'))
{
document.getElementById('two').value = defwepkey;
}
else
{
alert("Element with id two doesnt exists");
}
Avatar of adman08

ASKER

This results in 'Element w/ ID two doesn't exit.'
That means there is no ID attribute for that element.
Avatar of adman08

ASKER

Hmm, I need to review the rest of this to see what may be happening.  I have the ID attributes clearly listed in the input, but they're not being populated.  
Avatar of adman08

ASKER

Thanks for your help, I was able to re-build my JavaScript & HTML content so that it worked.  This was the best answer to my problem, as I searched web-wide and via Experts Exchange before asking.  
Avatar of adman08

ASKER

Thanks for your help, I was able to re-build my JavaScript & HTML content so that it worked.  This was the best answer to my problem, as I searched web-wide and via Experts Exchange before asking.  
Glad to know its fixed :)

Cheers
Kiranvj
No worries - glad ro help.