Jump to content

[SOLVED] using getelementbyname instead of getelementid


M.O.S. Studios

Recommended Posts

Hey guys,

 

i have this script that takes the information from this form and places it into another form (this script is ran withi a popup)

 

<html>
<head>
<script type="text/javascript">

function sendToPageOne()
{
    var thisValue = document.getElementById('image1').value;
    window.opener.form1.image1.value = thisValue;
}

</script>
</head>
<body>


  <table width="100%" height="100%" cellpadding="0" cellspacing="0" frame="0" border="0">
  <form method="POST" action="account.php?actions=addpic" enctype="multipart/form-data">
   <tr width='100%'><td width='100%' height='5'><a href='account.php?actions=addpic'><font size='2'><i>root</i></font></a></td></tr>

<tr width='100%'><td width='100%' height='5' bgcolor='#a4b9f7'><input type='submit' name='directory' value='upload/..' id='submitb'></td></tr>
<tr width='100%'><td width='100%' height='5' bgcolor='#779ff7'><input type='submit' name='directory' value='../../upload/cards' id='submitb'></td></tr>
<tr width='100%'><td width='100%' height='5' bgcolor='#779ff7'><input type='submit' name='directory' value='../../upload/test' id='submitb'></td></tr>
</form>

<form name="form2" action="browse.php" method="POST">

<tr width='100%'><td width='100%'>

  
  <input type="radio" name="image1" id=image1 value="" checked>None<br>
     <input type="radio" name="image1" id=image1 value="../../upload/1.jpg">1.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/2.jpg">2.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/2472178932_060c51e5a7[1].jpg">2472178932_060c51e5a7[1].jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/Crater.jpg">Crater.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/clistheader[1].jpg">clistheader[1].jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/cube.jpg">cube.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/gcube.jpg">gcube.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/test.jpg">test.jpg<br>
       <input type="submit" onclick="sendToPageOne();self.close();"; value="Send to page 1">
     
    </td>
   </tr>
  </form>
</table>



</body>
</html>

 

at first it wasn't radio buttons it was a select box, it worked fine

 

 

now i switched it to a radio buttons and its not working, i think that's because i should use GetElementname for a radio button.

 

how ever when i switch it to that i still have no luck

 

any ideas????

 

thanks in advance

Link to comment
Share on other sites

The problem has nothing to do with getElementById. The problem is that you are dealing with a radio group. With a select list you can find the value of the select list object. But, with a radio group each option has a value and there is no direct way to reference the selected value.

 

To add to the problem, a radio group is treated differently if it has multiple options or only one option. I have a small custom function that will return that selected value of a radio group regardless of the number of options:

 

function radioGroupValue(groupObj)
{
    //Check if radio group has multiple options (i.e. is an array)
    if (groupObj.length)
    {
        //Iterrate through each option
        for (var i=0; i<groupObj.length; i++)
        {
            //Check if option is checked
            if (groupObj[i].checked)
            {
                //Return value of the checked radio button
                return groupObj[i].value;
            }
        }
    }

    //There is only one option in the radio group
    else if (groupObj.checked==true)
    {
        return groupObj.value;
    }

    //No option was selected
    return false;
}

Link to comment
Share on other sites

1. JavaScript (which is what we are talking about) is not Java. Two different things.

 

2. No need to go back to a select list. Just need to incorporate the function I posted.

 

Try this:

<html>
<head>
<script type="text/javascript">

function radioGroupValue(groupObj)
{
    //Check if radio group has multiple options (i.e. is an array)
    if (groupObj.length)
    {
        //Iterrate through each option
        for (var i=0; i<groupObj.length; i++)
        {
            //Check if option is checked
            if (groupObj[i].checked)
            {
                //Return value of the checked radio button
                return groupObj[i].value;
            }
        }
    }

    //There is only one option in the radio group
    else if (groupObj.checked==true)
    {
        return groupObj.value;
    }

    //No option was selected
    return false;
}

function sendToPageOne()
{
    var thisValue = radioGroupValue(document.form2.image1);
    window.opener.form1.image1.value = thisValue;
}

</script>
</head>
<body>


  <table width="100%" height="100%" cellpadding="0" cellspacing="0" frame="0" border="0">
  <form method="POST" action="account.php?actions=addpic" enctype="multipart/form-data">
   <tr width='100%'><td width='100%' height='5'><a href='account.php?actions=addpic'><font size='2'><i>root</i></font></a></td></tr>

<tr width='100%'><td width='100%' height='5' bgcolor='#a4b9f7'><input type='submit' name='directory' value='upload/..' id='submitb'></td></tr>
<tr width='100%'><td width='100%' height='5' bgcolor='#779ff7'><input type='submit' name='directory' value='../../upload/cards' id='submitb'></td></tr>
<tr width='100%'><td width='100%' height='5' bgcolor='#779ff7'><input type='submit' name='directory' value='../../upload/test' id='submitb'></td></tr>
</form>

<form name="form2" action="browse.php" method="POST">

<tr width='100%'><td width='100%'>

  
  <input type="radio" name="image1" id=image1 value="" checked>None<br>
     <input type="radio" name="image1" id=image1 value="../../upload/1.jpg">1.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/2.jpg">2.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/2472178932_060c51e5a7[1].jpg">2472178932_060c51e5a7[1].jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/Crater.jpg">Crater.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/clistheader[1].jpg">clistheader[1].jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/cube.jpg">cube.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/gcube.jpg">gcube.jpg<br>
       <input type="radio" name="image1" id=image1 value="../../upload/test.jpg">test.jpg<br>
       <input type="submit" onclick="sendToPageOne();self.close();"; value="Send to page 1">     
    </td>
   </tr>
  </form>
</table>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.