SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Location
    Omaha, NE
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Radio Buttons and Response

    Hello again! I have moved to JavaScript and am having problems with an example. I have decided to try handling it bit by bit. The first issue I am dealing with, though, appears to be an HTML issue rather than a JavaScript issue.

    Specifically, I have a question, "Are you the President?", with two radio buttons labeled "Yes" and "No". If the user checks Yes, the browser should display the phrase "Mr. President". If the user checks No, the browser should display "Sir". I can get the Sir when I click yes, but I can't get the "Mr. President" when I click no. If I eliminate the Mr. and just have President, it works. The problem is that several examples in my book of both document.write quotes and alert quotes allow phrases that include punctuation and spaces. So why is it not working in this example?

    Here is the code. I have run it as both a document.write and an alert. FYI, the next step is to try to assign a value to a variable when I click a choice rather than do a doc write or an alert. I would then use the variable in my script.

    <html>
    <body>

    <form>
    <p>Are you the President?<br />
    <input type="radio" name="pres" value="no" onclick=alert("Sir") />No<br />
    <input type="radio" name="pres" value="yes" onclick=alert("Mr President") />Yes</p>
    </form>

    </body>
    </html>

    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Feb 2010
    Location
    Milan, OH
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html>
    <body>
    
    <form action="">
    <p>Are you the President?<br />
    <input type="radio" name="pres" value="no" onclick="alert('Sir')"/>No<br />
    <input type="radio" name="pres" value="yes" onclick="alert('Mr. President')" />Yes</p>
    </form>
    
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    May 2010
    Location
    Omaha, NE
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. Now - next question. I changed the alert to document.write. It gave me the text response I wanted, but it did so on a new page. Is there any way to make the text response appear on the same page as and below the question with the radio buttons?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dtarvin View Post
    Thank you. Now - next question. I changed the alert to document.write. It gave me the text response I wanted, but it did so on a new page. Is there any way to make the text response appear on the same page as and below the question with the radio buttons?
    document.write always writes to a new page if the current page has finished loading.

    There are two ways to update the current page. The standard way is to use the appropriate DOM commands, the alternative that most browsers support is to use innerHTML. If you use innerHTML you'll need a tag after the radio buttons to update with the new content. If you use DOM commands you can add the new content without needing anything extra there at the start.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •