Forum Moderators: open
I am new to using java script and am trying to get a variable I have called to be used in a form. I have a script that sets a number value to a variable. I am then using a slider on my website that uses a form. It has an option for a default value. In stead of hardcoding a value to that, I want it to use the javascript variable.
Here is a stripped down look at the code with what I am hoping is all the nonrelevant stuff stripped out:
<head>
<script type="text/javascript">
var currentVolume;
</script>
</head><body>
<form>
<input id="thesliderid" name="theslider"
class="fd_range_0_100 fd_classname_sliderclass fd_callback_volumeslider"
title="0 - 100" value="10">
<form>
</div>
</body>
What I am trying to do is take the line that has value="10" and replace the 10 with the var currentVolume.
Any suggestions?
Thank you in advance for your help.
You'd need to explain more carefully what it is you are trying to do, how your slider control operates, etc.
f=document.forms[formNumber/'formName']
f.formElementName.value
in your case:
document.forms[0].theslider.value
or
document.forms[0].elements[0].value
or if the field name as some weird letters (works at least in FF)
document.forms[0].elements.item('theslider').value
document.forms[0].elements.namedItem('theslider').value
[edited by: Achernar at 2:17 pm (utc) on Feb. 22, 2008]
So regardless of what the volume is set to, when a page is loaded with this slider, the button will automatically show up at the position that is equal to the "value=" field of the form. This doesn't affect the volume of the flash player until I slide it. What I am trying to do is to get the slider to use the actual volume that the player is at, which is stored in the currentVolume variable, instead of having it hardcoded as a number, like the 10 i gave in the example in the first post.
The code below works fine if I hardcode it to something like value="5", but what i am wanting to do is insert that javascript variable into this html form in the place of what the value equals so that it isn't a hardcoded number. That way the value= part of the form would always equal that of the javascript currentVolume var. For example something like this (only with the correct syntax to use the java script variable)
<form>
<input id="thesliderid" name="theslider"
class="fd_range_0_100 fd_classname_sliderclass fd_callback_volumeslider"
title="0 - 100" value="var currentVolume">
<form>
The currentVolume has a value set to it earlier in the script, this is a numeric value that can be anywhere from 0-100. This part is already working. I want the form's value="" tag to use that variable.
Thank you for your replies.
My main problem is I just don't understand how to call a predefined javascript variable inside html
You can't.
You can only reference a Javascript variable in Javascript.
If you want the content of a Javascript variable to appear on the page, or to set an attribute on an HTML element, then you will have to navigate the DOM (document object model) and programatically change the element that you want to change.
fside and Archernar gave you the answer.
<form>
<script type="text/javascript">
var string = '<input id="thesliderid" name="theslider" class="fd_range_0_100 fd_classname_sliderclass fd_callback_volumeslider" title="0 - 100" value="' + currentVolumewink + '">';
document.writeln(string);
</script>
<form>