Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question text field and select field dependence

    Hi guys! I want to create a form whereby whenever a value is selected from a list of select field it's corresponding value should appear on the text field.
    example,if the select field has a list of file numbers,then if a certain file number is selected the corresponding file name should apppear on the text field

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You should not cross-post. A response was made at your early thread:
    text field and select field dependence
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <form name = "myForm">
    <select name = "colour" style="width:160px" onchange = "writebox()">
    <option value = "Choose a Colour">Select a colour</option>
    <option value = "Red">Red</option>
    <option value = "Blue">Blue</option>
    <option value = "Green">Green</option>
    <option value = "Yellow">Yellow</option>
    </select>
    <br><br>
    <input type = "text" name = "txtfield" id = "txtfield" size = "20">
    </form>
    
    <script type = "text/javascript">
    function writebox() {
    var pick1 = document.myForm.colour.selectedIndex;
    if (pick1 == 0) {alert ("You must make a selection")}
    document.myForm.txtfield.value = document.myForm.colour.options(pick1).value; 
    }
    </script>
    1.2) Do not crosspost- CodingForums.com is divided into various categories.
    When initiating a thread, always locate the most appropriate category, and post your
    question only ONCE, in that category. Do not post the same question in multiple categories.
    Last edited by Philip M; 07-31-2008 at 10:45 AM. Reason: Noticed double post too late!

  • #4
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Its my first time in this forum guys.I tried to play with the codes U gave me but what i want is the value to be displayed on text field is different from the value selected from the select field which is different from what the codes u gave me is doing.I am a begginer to javascript

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You are not very clear. Is this what you want?

    Code:
    <form name = "myForm">
    <select name = "filename" style="width:160px" onchange = "writebox()">
    <option value = "You must choose a file">Select a file</option>
    <option value = "File 1 Name">File 1</option>
    <option value = "File 2 Name">File 2</option>
    <option value = "File 3 Name">File 3</option>
    <option value = "File 4 Name">File 4</option>
    </select>
    <br><br>
    <input type = "text" name = "txtfield" id = "txtfield" size = "20">
    </form>
    
    
    <script type = "text/javascript">
    function writebox() {
    var pick1 = document.myForm.filename.selectedIndex;
    if (pick1 == 0) {alert ("You must make a selection")}
    document.myForm.txtfield.value = document.myForm.filename.options(pick1).value; 
    }
    </script>

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Should be closing and opening bracket instead:
    Code:
    document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;
    Maybe I got your point, what Philip M has given would meet your requirement, you just need to replace highlighted to the corresponding values you wish:
    Code:
    <option value = "You must choose a file">Select a file</option>
    <option value = "File 1 Name">File 1</option>
    <option value = "File 2 Name">File 2</option>
    <option value = "File 3 Name">File 3</option>
    <option value = "File 4 Name">File 4</option>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by rangana View Post
    Should be closing and opening bracket instead:
    Code:
    document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;
    How is that different from:

    document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;



  • #8
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    why not?

    Code:
    function writebox() {
    document.myForm.txtfield.value = document.myForm.filename.value;
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #9
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Quote Originally Posted by Philip M View Post
    How is that different from:

    document.myForm.txtfield.value = document.myForm.filename.options(pick1).value;


    It only works on IE.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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