Forum Moderators: open

Message Too Old, No Replies

JavaScript document.form question

         

ryan_b83

5:39 pm on Oct 2, 2006 (gmt 0)

10+ Year Member



Hello, I am having problems with the following code. I am trying to access a dynamic form name through the document object. The reason why it is dynamic is because each form created is from a database.

There will be 3 dynamicly created forms from a database for example

<form name='form443' action='' method='post'>
code...
</form>

<form name='form442' action='' method='post'>
code...
</form>

<form name='form441' action='' method='post'>
code...
</form>

The problem lies in trying to access these forms within Javascript. The following code has an error in it when i try accessing each form dynamiclly? Any ideas?

<script type='text/javascript'>
function shipping_method($sid){
alert(document.form+$sid.radio_shipping_method+$sid.value);

}
</script>

Thank,
Ryan

Fotiman

6:23 pm on Oct 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this instead:

alert(document.forms['form'+$sid].elements['radio_shipping_method'+$sid].value);

If that doesn't work, then try using eval.

ryan_b83

7:17 pm on Oct 2, 2006 (gmt 0)

10+ Year Member



Tried that, and it said returned an alert saying 'undefined'

i also tried this
alert(document.eval('form'+$sid).eval('radio_shipping_method'+$sid).value);

and it also returend 'undefined'

this is the input varible it is referring to.

<input onclick="shipping_method(<? echo $shipping_row['sid'];?>);" type='radio' name='radio_shipping_method<? echo $shipping_row['sid'];?>' id='radio_shipping_method<? echo $shipping_row['sid'];?>' value='d' />

Fotiman

7:33 pm on Oct 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In that case, maybe try throwing in some debug alert statements to see what's really there.



var f = document.forms['form'+$sid];
if(!f )
{
alert("Unable to find form: 'form" + $sid + "'");
}
if( f )
{
alert("f.elements.length = " + f.elements.length);
var r = f.elements['radio_shipping_method' + $sid ];
if(!r )
{
alert("Unable to find element: 'radio_shipping_method" + $sid + "'");
}
}

foobar

5:08 am on Oct 3, 2006 (gmt 0)

10+ Year Member



Another option would be to use id's on the form (instead of names, or you can use both if you need to), and then grab the object via the id. Also, if you're already naming the form, why does the name of the radio button have to include the sid as well? Also, I'm pretty sure a dollar sign is not a valid char to have in a JS variable.

<form name='form443' id='form443' action='' method='post'> code... </form>
<form name='form442' id='form442' action='' method='post'> code... </form>
<form name='form441' id='form441' action='' method='post'> code... </form>

<script type='text/javascript'>
function shipping_method (sid) {
var el = document.getElementById('form'+sid);
alert (el.radio_shipping_method.value);
}
</script>