Solved

Can I use the onClick event for plain text?

Posted on 2004-04-21
7
348 Views
Last Modified: 2012-06-27
Hi,

trying to call a function through the onClick event of text.
Another question is, can I declare the variable numsquare this way (inside the alert mehtod)?

I want to link text to the function. Something like;

<onclick=squareit(), "Square any number">

Am I at least close? :-(

Here's the function I wrote:

<script language="JavaScript" type=text/javascript>
<!--//
function squareit()
{
alert(var numsquare = prompt("Please enter in any number to be squared.") + "Thank you, " + numsquare + " squared is " + numsquare * numsquare ".")
}
//-->
</script>


TIA,
Tony
0
Comment
Question by:Tony_A
  • 4
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
jaysolomon earned 75 total points
ID: 10880767
<html>
<head>
<title></title>
<script type="text/javascript">
<!--
function squareit()
{
var numsquare = prompt("Please enter in any number to be squared.",0)
alert("Thank you, " + numsquare + " squared is " + numsquare * numsquare +".")
}
// -->
</script>
</head>
<body>
<span onclick="squareit();">"Square any number"</span>
</body>
</html>
0
 

Author Comment

by:Tony_A
ID: 10880822
Works great! But you already knew that! :-P

So would the variable declaration not have worked?
Was it just plain old "bad form" to try to put all that in the alert() method?

Thanks again,
Tony
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10880859
you cannot have var ... = prompt(.......... in a alert.

You should declare all variables either local or global and then in the alert you can show them as i did above.

Glad to have helped and thanks
jAy
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 16

Expert Comment

by:jaysolomon
ID: 10880860
But why a B?
0
 

Author Comment

by:Tony_A
ID: 10880915
thought you might ask...

I guess cause while you gave me code that worked, I have just as much desire to understand
why I am wrong. I try to use my reference materials for these answers, but they aren't clear about why things are done the way they are.

Such as "cannot have var ... = prompt(.......... in a alert."

Those answers are really important to me too.
Thanks again for help!
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10887929
Thanks

jAy
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now