SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic Selection Box That Writes An Answer

    Hi All

    I am not that advanced with JavaScript but learning everyday.

    I have a JavaScript that writes the selection of a dropdown box into a textbox.

    I want to add it to a PHP page that will add this script for every result in the database.

    If I have only 1 entry the script works fine:

    HTML Code:
    <script language="JavaScript">function insertMenuItem(){
    
        document.f.name.value = document.f.insertItem.options[document.f.insertItem.selectedIndex].text;
        document.f.link.value = document.f.insertItem.value;
    
    }</script>
    
    <input type=text value='' name=link size=25>
    
    <select name=insertItem><option value=layout-1.php?ID=4>Layout 1</option><option value=layout-2.php?ID=4>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>
    If I add another it no longer works:

    HTML Code:
    <script language="JavaScript">function insertMenuItem(){
    
        document.f.name.value = document.f.insertItem.options[document.f.insertItem.selectedIndex].text;
        document.f.link.value = document.f.insertItem.value;
    
    }</script>
    
    <input type=text value='' name=link size=25>
    
    <select name=insertItem><option value=layout-1.php?ID=4>Layout 1</option><option value=layout-2.php?ID=4>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>
    
    <select name=insertItem><option value=layout-1.php?ID=4>Layout 1</option><option value=layout-2.php?ID=4>Layout 2</option></select> <input type=button value='Insert' onclick=insertMenuItem()>
    I need to get this script working so there is the option of having may dropdown boxes that will all write to the link box when the insert button is clicked.

    Any help or suggestions would be great.

    Many Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Someone... PLEASE HELP

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any time you give two form elements the same name, the references to them found at
    ref_to_form.element_name and ref_to_form.elements.element_name are packaged into an array,
    the standard way of storing multiple related values. You now need to reference them as:
    Code:
    document.f.insertItem[n].options[document.f.insertItem[n].selectedIndex].text;
    ...where n is the order of the particular element in its 'group', reading the HTML source down from the top, and starting with zero.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    function insertMenuItem(n)
    {
    	var oSelect = document.f.insertItem[n];
    	var whichlink = document.f.link[n];
    	whichlink.value = oSelect.options[oSelect.selectedIndex].value;
    }
    
    </script>
    </head>
    <body>
    <form name="f">
    <select name="insertItem">
    <option value="layout-1.php?ID=4">Layout 1</option>
    <option value="layout-2.php?ID=4">Layout 2</option>
    </select>
    <input type="button" value="Insert" onclick="insertMenuItem(0)">
    <input type="text" name="link" value="" size="25">
    
    <br /><br />
    <select name="insertItem">
    <option value="layout-1.php?ID=4">Layout 1</option>
    <option value="layout-2.php?ID=4">Layout 2</option>
    </select>
    <input type="button" value="Insert" onclick="insertMenuItem(1)">
    <input type="text" name="link" value="" size="25">
    
    </form>
    </body>
    </html>
    Just a guess as it's really unclear what you want to do....
    Last edited by adios; Mar 18, 2004 at 15:03.
    ::: certified wild guess :::


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •