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.
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Still not there yet

    I made the change, but I'm still not seeing the ItemPrice1 field populate when a selection is made:

    <form>
    Product Name
    <input type="text" name="ItemPrice1" /><select id="ItemName1" name="ItemName1"><option value="none">--choose one--</option><option value="English Teacher">English Teacher ($32,000)</option><option value="Math Teacher">Math Teacher ($45,000)</option><option value="Physics Teacher">Physics Teacher ($60,000)</option><option value="Programming Teacher">Programming Teacher (priceless)</option></select>


    <script type="text/javascript">// <![CDATA[
    ( function( ) { /* notice the underlines in the names here: */ var prices = { "none" : 0, "English_Teacher" : 32000, "Math_Teacher" : 45000, "Physics_Teacher" : 60000, "Programming_Teacher" : 0.01 }; var sel = document.getElementById("ItemName1"); sel.onchange = function( ) { sel.form.ItemPrice1.value = prices[ sel.value.replace(/\s/g, "_") ]; } } )();
    // ]]></script>

    </form>

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Have you never heard of code formatting? You really like your HTML all jumbled onto one line like that???

    Anyway, even with the ugly code it worked for me.

    Oh, wait! You have your </form> tag *after* the <script>!!!

    NO NO NO!! When you do that, the <form> is NOT COMPLETE and so then this code:
    Code:
        sel.form.ItemPrice1.value = ...
    doesn't work, because sel.form will be null, since the </form> tag has not yet been seen.

    Just move the </form> tag to BEFORE the <script> tag.

    As I said, the best place for the <script>...</script> is *JUST* before the </body>.

    Anyway, tested like this and it works fine:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form>
    Product Name
    <input type="text" name="ItemPrice1" /><select id="ItemName1" name="ItemName1"><option value="none">--choose one--</option><option 
    
    value="English Teacher">English Teacher ($32,000)</option><option value="Math Teacher">Math Teacher ($45,000)</option><option value="Physics 
    
    Teacher">Physics Teacher ($60,000)</option><option value="Programming Teacher">Programming Teacher (priceless)</option></select>
    </form>
    
    <script type="text/javascript">// <![CDATA[
    ( function( ) { /* notice the underlines in the names here: */ var prices = { "none" : 0, "English_Teacher" : 32000, "Math_Teacher" : 45000, 
    
    "Physics_Teacher" : 60000, "Programming_Teacher" : 0.01 }; var sel = document.getElementById("ItemName1"); sel.onchange = function( ) { 
    
    sel.form.ItemPrice1.value = prices[ sel.value.replace(/\s/g, "_") ]; } } )();
    // ]]></script>
    
    </body>
    </html>
    NOTE: Unless you are using XML, instead of HTML, you don't need the // <![CDATA[ stuff.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,033
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by Old Pedant View Post
    Oh, wait! You have your </form> tag *after* the <script>!!!

    NO NO NO!! When you do that, the <form> is NOT COMPLETE and so then this code:
    Code:
        sel.form.ItemPrice1.value = ...
    doesn't work, because sel.form will be null, since the </form> tag has not yet been seen.
    This shouldn't matter, sel.form would not be null. But even if this does, sel.form would still work because it is called inside onchange event which implies the form has been loaded completely.

    BTW, I copied the code and it works for me locally.

  4. #19
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    You are right. By the time the function is called, the <form> is complete in any case.

    I copied his code but moved the </form>. Didn't think to try it without doing so.

    Only thing I can guess is that he has a second element on the page with the same ID as the <select>. So then getElementById() would fail.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  5. #20
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Still not working

    I really appreciate the attention you are giving this.

    1. I don't like the way the code looks on one line and I don't know how to format it. How do you do it?

    try this link:

    http://www.world-affairs.org/?page_id=4523&preview=true

  6. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    By putting line breaks in between each line of code to start with.

    To truly format it in these forums, put a [ code ] tag before the code and a [ /code ] tag after the code (both of them without spaces in them of course).

    *********

    I went to the page you linked to and it seems to just give a 404 error. In any case, there is no <select> anywhere on that page.

    Oh...and howdy, neighbor. I'm in Snohomish.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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