www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] what am I missing? simple function.

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    82

    resolved [RESOLVED] what am I missing? simple function.

    does anyone have any idea why this function doesn't work????

    function chck(ans){
    var userin = document.getElementById(ans).value;
    if (userin == ans) {
    alert("correct!");
    }
    }

    html (written as part of a javascript function):
    <form action=\"chck(' "+letters+" ')\"><input type=\"text\" id=\""+letters+"\" value=\"answer here (no caps)\"/>\
    <input type=\"submit\"></form>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,088
    I would say it's the added spaces here:
    Code:
    (' "+letters+" ')\
    cf:
    Code:
    ('"+letters+"')\
    this simplified example seems to work ok:
    Code:
    <body>
    <script type="text/javascript">
    var letters="abc"
    document.body.innerHTML="<form><input type='text' id="+letters+" value='answer here (no caps)'/><input type='button' value='check' onclick=\"chck('"+letters+"')\"/></form>";
    function chck(ans){
    var userin = document.getElementById(ans).value;
    if (userin == ans) {
    alert("correct!");
    }
    }
    </script>
    </body>

  3. #3
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    82
    OH MY FREAKIN HECK... wow I can't believe if missed that.. this was just a very small part of a single script 3-400 lines long that I've been working on for weeks... but thank you a million times over for that suggestion... lol i feel dumb now cause I should have noticed that a long time ago... I do have one question, though... why are single quotes not required around the double quotes in the id="+letters+" part? I kept them in originally because I thought that that it would be required to follow HTML syntax.
    Last edited by thewebiphyer; 09-29-2012 at 07:05 PM.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,088
    you never got a "that element is null" type error?

  5. #5
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    82
    nope, but then again i usually use chrome

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles