PDA

View Full Version : document.forms[0].options?


dreamingdigital
12-11-2002, 04:28 PM
Hi

As usual I have a bit of a challenging question.

I can't change the names of the radio buttons in the form I'm using (it's from an e-comm program called Miva). I know the form which I'm trying to access: document.forms[0]

Ok here is the problem. The radio buttons I am trying to access via JavaScript all have the name "AttributeValue[2]". See a problem? Yeah, the [2] part. So that screws up any JS trying to access it.

I think I can get around this because there is only one set of radio buttons on the page and those are the ones I want to access...

I need the script for document.forms[0].radiobuttonset[1]... ?? or is it .options??

In other words, how do I access the only set of radio buttons on the page without using their name.

Colin

beetle
12-11-2002, 04:31 PM
Use the elements collection

document.forms[0].elements['AttributeValue[2]']

should work :D

requestcode
12-11-2002, 04:34 PM
You will want to use document.forms[0].elements to access the form elements. You could create a for loop to go through the form to check for the element type and then do some further processing based on that. Here is an example:
for(i=0;i<document.forms[0].length;i++)
{
if(document.forms[0].elements[i].type="radio")
{
then do something
}
}

document.forms[0].length will give the the number of elements in your form. If the form element has a value associated with it then you can get that in the "for loop" this way:
document.forms[0].elements[i].value

dreamingdigital
12-11-2002, 05:04 PM
Thanks for the help. I incorporated your scripts into my function. It still looks like I can't get around that name issue with the [2] part though. I get an error saying document.forms.0.AttributeValue.1 is null or not an object which is true because the name of the radio button set is AttributeVaue[1]. Here is the code I wrote: And here is the URL of the site if you want to view source. I wish I could change the name of the radio buttons but I can't. http://www.granisle.com/miva/merchant.mv?Screen=PROD&Store_Code=granisle&Product_Code=152&Category_Code=eruption_cold


function preview() {

var message = document.forms[0].AttributeValue[1].value;
var font= "courier";

for (var i=0; i<document.forms[0].length; i++) {
if (document.forms[0].elements[i].type="radio") {
if (document.forms[0].elements[i].checked)
font = document.forms[0].elements[i].value;
}
}

if (font == "Font_Let_Us_Chose")
font = "Arial,sans-serif";

popup('about:blank','pre','550','550');
pre.document.open();
pre.document.write('<html><head><title>Font Preview</title></head>');
pre.document.write('<body bgcolor=white text=black><font size=4>');
pre.document.write('<font face=\"'+ font +'\">'+ message +'</font>');
pre.document.write('</body></html>');
pre.document.close();

}

Colin

requestcode
12-11-2002, 05:41 PM
What element type is document.forms[0].AttributeValue[1]?

If it is a text box and you know that it will always be the second element (relative to zero) then you could refrence it this way:
message=document.forms[0].elements[1].value .

You could also use a for loop to check for it and grab the value that way.

dreamingdigital
12-11-2002, 06:26 PM
Thanks for the help. You're awesome.

Here's what I finally came up with. It's not pretty but it works.

function preview() {

var message = document.forms[0].elements[6].value;
var font= "courier";

if (document.forms[0].elements[7].checked) font = document.forms[0].elements[7].value;
else if (document.forms[0].elements[8].checked) font = document.forms[0].elements[8].value;
else if (document.forms[0].elements[9].checked) font = document.forms[0].elements[9].value;
else if (document.forms[0].elements[10].checked) font = document.forms[0].elements[10].value;
else if (document.forms[0].elements[11].checked) font = document.forms[0].elements[11].value;
else if (document.forms[0].elements[12].checked) font = document.forms[0].elements[12].value;
else if (document.forms[0].elements[13].checked) font = document.forms[0].elements[13].value;
else if (document.forms[0].elements[14].checked) font = document.forms[0].elements[14].value;
else if (document.forms[0].elements[15].checked) font = document.forms[0].elements[15].value;
else if (document.forms[0].elements[16].checked) font = document.forms[0].elements[16].value;


if (font == "Font_Let_Us_Chose")
font = "Arial,sans-serif";

var pre=null;

var url = 'about:blank';
var target = 'pre'
var w = '550'
var h = '550'

LeftPosition=(screen.width)?(screen.width-w)/2:100;
TopPosition=(screen.height)?(screen.height-h)/2:100;

settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location =no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';

pre = window.open(url,target,settings);

pre.document.open();
pre.document.write('<html><head><title>Font Preview</title></head>');
pre.document.write('<body bgcolor=white text=black><font size=4>');
pre.document.write('<font face=\"'+ font +'\">'+ message +'</font>');
pre.document.write('</body></html>');
pre.document.close();

}

Take care,
Colin

beetle
12-11-2002, 07:00 PM
Originally posted by dreamingdigital
It still looks like I can't get around that name issue with the [2] part though. :( Sniff sniff :( Did no one read my first post?<html>
<body>

<script language="javascript">

function alerts(f) {
alert(f.elements['AttributeValue[2]'].value);
}

</script>

<body>

<form>
<input type="text" name="AttributeValue[2]" />
<input type="button" value="Show Value" onClick="alerts(this.form)" />
</form>

</body>
</html>

dreamingdigital
12-11-2002, 08:08 PM
I read it. You were very helpful. :)