Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
16
rated 0 times [  18] [ 2]  / answers: 1 / hits: 15640  / 15 Years ago, thu, january 14, 2010, 12:00:00

I'm trying to change the text inside a h1 tag using the element id greeting and a external java script. I'm having trouble trying to figure out how to call upon my function init that has gathered variables and use them in the next function. Heres what I have so far.



HTML



 <head>

<title>Matt Beckley</title>
<script type=text/javascript src=script1.js></script>
</head>

<body>

<h1 id=greeting>Welcome!</h1>

</body>
</html>


THE SCRIPT WHERE THE PROBLEM IS!



 window.onload = init;

function init()
{
var fullname=prompt(Please Enter you name!,Anonymous );
(fullname == Anonymous)? Anonymous : ;

var nickname=prompt(Please Enter you nickname!, None);
(nickname == None)? None: ;
}

function writeGreeting()
{
document.getElementById(greeting).innerHTML= ?????????
}


So I don't know how to make the last function work so that the h1 tag shows a name and a nick. w3schools only shows Strings and not the ability to call the other function. Anyone know how to do this?


More From » javascript

 Answers
27

How about merging the init and writeGreeting functions:



window.onload = init;

function init()
{
var fullname=prompt(Please Enter you name!,Anonymous );
fullname = (fullname == Anonymous)? Anonymous : ;

var nickname=prompt(Please Enter you nickname!, None);
nickname = (nickname == None)? None: ;
document.getElementById(greeting).innerHTML= Welcome + fullname + ( + nickname + );
}


p.s. Minor point of grammar - you probably want Please enter your name, rather than Please Enter you name, and similarly for the nickname prompt.


[#97834] Monday, January 11, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;