tags:

views:

133

answers:

7

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?

+1  A: 

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.

Dominic Rodger
A: 
// Create two GLOBAL variables
var fullname = "";
var nickname = "";

function init() {
  fullname = /* ... */
  nickname = /* ... */
  // pass the nickname on to the greeting function
  writeGreeting(nickname);
}

// This function is remain separate, if you wish to call it again
// at a later time before a page-refresh.
function writeGreeting(name) {
  document.getElementById("greeting").innerHTML = name;
}
Jonathan Sampson
A: 

How about like this?

It will display "Welcome fullname!" or "Welcome nickname!" or "Welcome Anonymous!" if you don't put any.

function init() {
     var fullname=prompt("Please Enter you name!");
     var nickname=prompt("Please Enter you nickname!");
     writeGreeting(fullname||nickname||"Anonymous");
}

function writeGreeting(name){
    document.getElementById("greeting").innerHTML= "Welcome "+name+"!";
}
S.Mark
A: 

I like Dominic's solution. Just bear in mind that you should be careful with any data entered by a user. I would recommend escaping string with escape() function, prior to using it.

e.g.

document.getElementById("greeting").innerHTML= "Welcome " + escape( fullname ) +
                                               " (" + escape( nickname ) + ")";
Vladimir Kocjancic
bobince
Ah. Yet, you have to agree that it is still better than nothing. :)
Vladimir Kocjancic
+1  A: 

Why not use .inner​Text instead?

burak ozdogan
Because it's IE-only. On other recent browsers you can use the DOM Level 3 Core property `textContent` instead. But yes, you definitely don't want to be writing unescaped text to `innerHTML`.
bobince
A: 

Try this. Tested with ie, chrome and ff (all latest).

var msg = "Welcome " + fullname + "(" + nickname + ")!";

var greetingElement = document.getElementById("greeting");

if(greetingElement.firstChild == null)
    greetingElement.appendChild(document.createTextNode(msg));
else
    greetingElement.firstChild.nodeValue = msg;
gp
A: 

how are you? please help me i have project talk about school and i want to make login for the student and teacher to reterive schedual thanks alote

Alaa