Solved

Can I use the onClick event for plain text?

Posted on 2004-04-21
7
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

728 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