Jump to content

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>
<html>
<body>

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

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

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

<script>

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

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

  document.write("<br><br><br><br>")

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; 
}
</script>

</body>
</html>

You can copy the code and wee for yourself.

Please advise.

 

Link to comment
https://forums.phpfreaks.com/topic/308025-some-insight-and-information-please/
Share on other sites

I added quotes to the TYPE attribute, but nothing changed.  Adding either single or double quotes to the ALERT render it totally inoperable.

document.write("<input type='radio' onclick=alert('Hello World');> Click here and alert FAILS")

Can you be more specific?

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.

Javascript is quite capable of putting double quotes inside of double quotes. Can you guess how? You'll have to do it either for the onclick's value, or by using single quotes for that and putting the double quotes inside the onclick code.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.