Solved

Can I use the onClick event for plain text?

Posted on 2004-04-21
7
349 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

863 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

18 Experts available now in Live!

Get 1:1 Help Now