Jump to content

Some insight and information please


Recommended Posts

I have two nearly identical radio button inputs that trigger JS alerts. 

The alert message will ONLY work when there are NO SPACES in the message. 

What am I doing wrong? 

What is the solution so that a message WITH spaces will be visible?

<!DOCTYPE html>

<p>Click the button get the text content of the button element.</p>

<button onclick="myFunction()" id="myBtn">Tryit</button>

<p id="demo"></p>


function myFunction() {
    var x = document.getElementById("myBtn").textContent;

document.write("<input type=radio onclick=alert('HelloWorld');> Click here and alert works")


document.write("<input type=radio onclick=alert('Hello World');> Click here and alert FAILS")
                                 //the ALERT will FAIL because of the SPACE between "Hello World"
 document.getElementById("demo").innerHTML = x; 


You can copy the code and wee for yourself.

Please advise.


Link to comment
Share on other sites

Why do you think that adding quotes to the type attribute will have any impact on what the onclick does?

HTML attributes need quotes around the values. Your onclick does not have quotes. You need to figure out a way to put quotes there. Remember you have two types of quotes available to use.

Link to comment
Share on other sites

Not having much luck with double quotes inside the double quotes.  It seems as though the script is interpreting the second double quotes as the end of the statement rather than a continuation.

Am I missing some sort of syntax rule? Is there a requirement of double-single-double (or similar) in order to work along those lines?

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.