Hello, I want to be able to click a link and have a input prompt pop up followed by an alert and then right the contents of the prompt to a specific spot. heres my code so far:
Code:
<html>
<head>
<script language="JavaScript">
function firstName() {
var name = prompt ("Please Enter Your Name");
alert('blah blah blah');
}
</script>
</head>
<body>
<a href="#blah" onClick="firstName()">Click</a>
<script language="JavaScript">document.write(name);</script>
</body>
</html>
but my problem is when I type something in, and hit ok on the prompt and ok on the alert, the output is "m" and then some random numbers. can i possibly fix this?
Last edited by bholtiphone; 11-18-2008 at 11:20 PM.
Why do you not use document.write in same function like
<html>
<head>
<script language="JavaScript">
function firstName() {
var name = prompt ("Please Enter Your Name");
alert('blah blah blah');
document.write(name);
}
</script>
1) Unless specified otherwise, all scripts are run when the page is loaded, so your document.write would actually execute before user get a chance to click on the link.
2) Your onclick doesn't actually cancel the default hyperlink-click action, which is to reload the page at "#blah". (Meaning anything done on this page is naught) You need to change it to
3) I am not sure if you want to use document.write, it is a very specialized method that is mostly used for ad syndication and pop-ups. Not designed for dynamic webpages.
ok is it possible to do this any other way then? i want a link that goes to an anchor and when clicked a prompt and then an alert should pop up. then the contents of the prompt would be displayed somewhere i specify. and i would like to do this all in javascript.
Put that div where ever you want the name to appear, and it is also where the page will jump to. Change the id (blah) to something else if you want to change that behaviour.
Code:
<script type="text/javascript">function firstName() {
var name = prompt ("Please Enter Your Name");
document.getElementById("blah").innerHTML = name;
}</script>
And a quick note, a link with "#blah" as the href doesn't actually reload the page, just jumps down along it.
Great wit and madness are near allied, and fine a line their bounds divide.
Thank you!! one question though. is it possible to output this to multiple places on the page? because i tried and it only showed up on one of the spots i put it
Bookmarks