Solved

Can I use the onClick event for plain text?

Posted on 2004-04-21
7
351 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…
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…

810 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