SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form elements not appearing in NS

    This code works in IE but not in Netscape. The form elements do not show up. I'm new to javascript and this code I found is from msdn.
    I want the textbox to be filled according to which radio button is clicked.

    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JScript">
    function CookieGroup()
    {
    document.getElementsByName("txtOutput").value = window.event.srcElement.value;
    }
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <form>
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=Cookies VALUE="I am the champion"
    onclick="CookieGroup()">
    Yep<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=NoCookies VALUE="better luck next time"
    onclick="CookieGroup()">
    Maybe<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=None VALUE="None" CHECKED
    onclick="CookieGroup()">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME=txtOutput STYLE="width:250"></TEXTAREA>
    </form>
    </BODY>
    </html>

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually I lied...the above code doesn't work in IE either! I forgot that I made changes without testing. Here is the code that works in IE without the form and without the document.getElementsByName

    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JScript">
    function CookieGroup()
    {
    txtOutput.value = window.event.srcElement.value;
    }
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=Cookies VALUE="I am the champion"
    onclick="CookieGroup()">
    Yep<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=NoCookies VALUE="better luck next time"
    onclick="CookieGroup()">
    Maybe<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=None VALUE="None" CHECKED
    onclick="CookieGroup()">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME=txtOutput STYLE="width:250"></TEXTAREA>
    </BODY>
    </html>

    Sorry about that!

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, first of all, 'JScript' a declaration for IE only, so change that to 'javascript'

    Second, earlier versions of NS don't support the EVENT object too well, so just remove the use of that like this:
    Code:
    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    function CookieGroup(newText)
    {
    txtOutput.value = newText;
    }
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=Cookies VALUE="I am the champion"
    onclick="CookieGroup(this.value)">
    Yep<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=NoCookies VALUE="better luck next time"
    onclick="CookieGroup(this.value)">
    Maybe<BR>
    <INPUT TYPE=radio NAME=rdoTest ID=None VALUE="None" CHECKED
    onclick="CookieGroup(this.value)">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME=txtOutput STYLE="width:250"></TEXTAREA>
    </BODY>
    </html>
    That *should* work.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The form elements still don't show up in Netscape...maybe it's the version I'm using? I changed to javascript and took out the event stuff.

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try surrounding all the attribute values in quotes, like this:
    Code:
    <INPUT TYPE="radio" NAME="rdoTest" ID="Cookies" VALUE="I am the champion"
    onclick="CookieGroup(this.value)">
    Also, older versions of NS may have a problem with both the NAME and ID attribute being on one object.

    EDIT: Oh, and you may want to add a <FORM> tag too...
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this actually anything to do with Javascript?

    I don't think so - i think it's html syntax.

    Just a thought.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still no luck. I took out ID, added quotes everywhere, and added the form tags. No form elements show up in Netscape and now I get a runtime error when I click a radio button in IE.
    This is the source that appears in NS:

    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    function CookieGroup(newText)
    {
    txtOutput.value = newText;
    }
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE="radio" NAME="rdoTest" ID="Cookies" VALUE="I am the champion"
    onclick="CookieGroup(this.value)">
    Yep<BR>
    <INPUT TYPE="radio" NAME="rdoTest" ID="NoCookies" VALUE="better luck next time"
    onclick="CookieGroup(this.value)">
    Maybe<BR>
    <INPUT TYPE="radio" NAME="rdoTest" ID="None" VALUE="None" CHECKED
    onclick="CookieGroup(this.value)">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME="txtOutput" STYLE="width:250"></TEXTAREA>
    </BODY>
    </html>

    I don't understand why the elements would show up in the page source but not on the page.

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flawless, take it easy on the n00bs. Besides, this was a javascript question at the start, sort of.

    Look like NS is caching that page, you said you make all those changes, but the source code you pasted in your last post still has no <FORM> tag and all the IDs. What version of IE gave you the error? And what's the error?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok - i was completely wrong - this IS a thing for Javascript - i wasn't reading properly.

    You will get an error in ie if it can't open the reference properly.

    Hide the js with:

    <!--

    //-->

    inside the beginning and end of the script tags.

    Try keeping the input tags on one line.

    Change the reference method for txtOutput.

    Flawless
    Last edited by Flawless_koder; Jul 30, 2002 at 07:58.
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is going to sound really dumb but what do you mean by:
    Originally posted by Flawless_koder

    Change the reference method for txtOutput.
    ?

    I'm getting the error "txtOutput is undefined".

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I mean:

    You are getting that error because you don't have a handle to the object.

    You need to get one.

    Depending on what systems you are working to, you can use:

    document.getElementById('objectname') so long as the object has an id.

    Failing that:
    document.object should do it - or document.forms['formname'].elements['elementName'] would do it.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I'm getting this error:

    'document.forms.box.elements' is null or not an object

    Here is my lastest code:
    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    <!--function CookieGroup(newText)
    {
    document.forms[0].elements['txtOutput'].value = newText;
    }//-->
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <form>
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="I am the champion" onclick="CookieGroup(this.value)">
    Yep<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="better luck next time" onclick="CookieGroup(this.value)">
    Maybe<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="none" CHECKED onclick="CookieGroup(this.value)">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME="txtOutput" STYLE="width:250"></TEXTAREA>
    </form>
    </BODY>
    </html>

    Does the error have something to do with this.value and newText?

  13. #13
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope - it means that the reference i gave you is bad.

    Can you tell me what versions this needs to work on?

    If it doesn't need to work on NS4 then give txtOutput an identical id to it's name .. and use

    document.getElementById('txtOutput').value = whatever;

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  14. #14
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which broswer gives you this error? And move the escaping comments to their own line in the code
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function CookieGroup(newText)
    	{
    	document.forms[0].elements['txtOutput'].value = newText;
    	}
    //-->
    </SCRIPT>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  15. #15
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm getting errors with ie and ns. NS gives and error at the bottom "javascript error: type javascript into the Location for details".

    I changed to

    document.getElementById('txtOutput').value = newText;

    and added Id="txtOutput" to the textarea line.

    I still get the same sort of error:

    document.getElementById(...) is null or not an object

    I also moved the escaping comments, which looks nicer, but didn't seem to help.

    BUT the form elements are showing up in netscape now. At least that's working!

  16. #16
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IT WORKS!!!

    The problem was the spaces between the " = ".

    document.getElementById('txtOutput').value = newText;

    I took the spaces out and now it works in ie.
    Thanks so much for your help everyone!!

  17. #17
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hooray!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  18. #18
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good.

    You should note also that the property is technically id - not Id.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  19. #19
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by beetle
    Hooray!
    Oh boy do you have FAR too much time on your hands

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  20. #20
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's my lunch hour, gimme a break!
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  21. #21
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Virginia, USA
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    working code for ie and ns

    This is the *final* code which works in ie AND ns 4. The escaping comments <!-- //--> made a big difference.

    <html>
    <HEAD>
    <title>hope this works</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function CookieGroup(newText)
    {
    document.box.txtOutput.value=newText;
    }
    //-->
    </SCRIPT>
    </HEAD>
    <BODY bgcolor="#FFFFFF">
    <form name="box">
    <!-- Controls are grouped by giving them the same NAME but unique IDs. -->
    Grouped Radio Buttons<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="I am the champion" onclick="CookieGroup(this.value)">
    Yep<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="better luck next time" onclick="CookieGroup(this.value)">
    Maybe<BR>
    <INPUT TYPE="radio" NAME="rdoTest" VALUE="none" CHECKED onclick="CookieGroup(this.value)">
    None<BR>
    Value of control on which the onclick event has fired<BR>
    <TEXTAREA NAME="txtOutput" STYLE="width:250"></TEXTAREA>
    </form>
    </BODY>
    </html>


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
  •