Forum Moderators: open

Message Too Old, No Replies

How do I can js var inside of html form?

javascript var form

         

greengeek

8:42 am on Feb 22, 2008 (gmt 0)

10+ Year Member



First want to say thanks for such a great site! Have found many answers in the past via google located on here.

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.

fside

12:44 pm on Feb 22, 2008 (gmt 0)

10+ Year Member



<html>
<head></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>
<script type="text/javascript">
currentVolume = 25;
document.getElementById("thesliderid").value = currentVolume;
</script>
</body>
</html>

You'd need to explain more carefully what it is you are trying to do, how your slider control operates, etc.

Achernar

2:12 pm on Feb 22, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Or, without ID on forms elements:

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]

greengeek

4:00 am on Feb 23, 2008 (gmt 0)

10+ Year Member



The slider is used to control the volume of a flash player I have. The form element there is what controls the slider and where the graphic for the button on the slider will show up.

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.

greengeek

4:06 am on Feb 23, 2008 (gmt 0)

10+ Year Member



My main problem is I just don't understand how to call a predefined javascript variable inside html, especially that of the form above.

greengeek

2:14 am on Feb 26, 2008 (gmt 0)

10+ Year Member



Any suggestions on how to go about doing this?

jtara

4:18 am on Feb 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

greengeek

6:58 am on Feb 26, 2008 (gmt 0)

10+ Year Member



fside seemed to be closest to what I am looking for, but he asked for more information, that is why I replied. Archernars is just to complex for my low level skills to even comprehend.

greengeek

7:10 am on Feb 26, 2008 (gmt 0)

10+ Year Member



Could I do something similar to this?


<form>
<input id="thesliderid" name="theslider"
class="fd_range_0_100 fd_classname_sliderclass fd_callback_volumeslider"
title="0 - 100" value="<script>document.write(currentVolume;)</script>">
<form>

Something along those lines, but with the correct syntax?

eelixduppy

8:16 pm on Feb 27, 2008 (gmt 0)



Welcome to WebmasterWorld! The following should get you what you want:

<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>