Results 1 to 5 of 5

Thread: extremely stuck

  1. #1
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default extremely stuck

    Hi guys,

    Im trying to have a drop down with values 1-5 and when a user clicks 2 for example it then automatically puts in 2 input fields and if 3 then three input fields etc.. here is my code but its not working correctly can anyone see why?


    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    function showdivs() {
    totaldivs = document.forms.consult.sales.value;
    totaldivs++;
    for (j=1; j<6; j++) { document.getElementById("sales" + j).style.display = 'none'; }
    for (i=1; i<totaldivs; i++) { document.getElementById("sales" + i).style.display = 'block'; }
    }
    </script>
    </head>
    <body>
    <form name="consult" id="consult" >
    <table>
    <tr>
    <td ><select name="sales" id="sales" onChange="showdivs();">
    <option value="0">Sales</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select></td>


    <div name="sales" id="sales" style="display: none;">
    </tr>
    <td><input name="sales_names" /></td>
    </tr>
    </div>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You cannot have two elements with an id of sales, and where's sales1, 2, etc?

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    You cannot have two elements with an id of sales, and where's sales1, 2, etc?

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    Hi sorry i dont have a live link unfortunately. The sales 1, 2 im trying to do in php

    foreach(div in divs){
    $i++
    <input name="sales_names($i)>

    }

  4. #4
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    You cannot have two elements with an id of sales, and where's sales1, 2, etc?

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    Im not entirely sure on how to get it fully working?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Here's one way to do something like that:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    #sales input {
    display:block;
    }
    #consult {
    display:none;
    }
    </style>
    <script type="text/javascript">
    document.write('<style type="text/css">#sales input {display:none;} #consult {display:block;}<\/style>');
    function showInputs(sel) {
    var totalInputs = document.getElementById('sales').getElementsByTagName('input');
    for (var i = totalInputs.length-1; i > -1; --i)
    totalInputs[i].style.display='none';
    for (var i = totalInputs.length-1; i > -1; --i)
    if(sel.value==i+1)
    totalInputs[i].style.display='block';
    }
    </script>
    </head>
    <body>
    <form action="#">
    <div id="sales"> 
    <select id="consult" onchange="showInputs(this);">
    <option value="0">Sales </option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    
    <input name="sales1">
    <input name="sales2">
    <input value="I'm 3" name="sales3">
    <input name="sales4">
    <input name="sales5">
    </div>
    </form>
    If javascript is disabled, all of the inputs and only the inputs will be seen. I put "I'm 3" as a value for sales3 for testing/demo purposes only. In actual practice, this is still not practical for any real life purpose I can think of, but might get you on your way.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •