phppup Posted December 17, 2018 Share Posted December 17, 2018 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 Share on other sites More sharing options...
Barand Posted December 17, 2018 Share Posted December 17, 2018 Put attribute value inside quotes. Link to comment Share on other sites More sharing options...
phppup Posted December 17, 2018 Author Share Posted December 17, 2018 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? Link to comment Share on other sites More sharing options...
requinix Posted December 17, 2018 Share Posted December 17, 2018 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 More sharing options...
Barand Posted December 17, 2018 Share Posted December 17, 2018 What about the onclick attribute? Especially so as that's the one giving you the problem! At the moment it thinks it's ""alert('Hello" Link to comment Share on other sites More sharing options...
phppup Posted December 17, 2018 Author Share Posted December 17, 2018 No combination of single or double quotes is correcting the situation. In fact, it tends to render the alert inoperable. Link to comment Share on other sites More sharing options...
requinix Posted December 17, 2018 Share Posted December 17, 2018 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. Link to comment Share on other sites More sharing options...
phppup Posted December 19, 2018 Author Share Posted December 19, 2018 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 More sharing options...
Barand Posted December 19, 2018 Share Posted December 19, 2018 Example for you alert("My name is \"phppup\".") Try it. (30 seconds of googling would have given you the answer) Link to comment Share on other sites More sharing options...
phppup Posted December 19, 2018 Author Share Posted December 19, 2018 Works GREAT!!!! Thanks again. [yes, did internet search last night and got a better understanding] Enjoy the holiday cheer. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.