MintY
MintY

Reputation: 643

How to append the variable value to the element using document.write

I am trying to append the variable value to the element

what i am doing is i am taking the input body onload by using javascript prompt.

now i am storing the input value to one variable.

now i want to append the variable value to this element

<a href="#" class="uname">Welcome User!</a>

in place of welcome user,i want to get the input variable value.

here is the code i am using..

function inpt(){
    var person=prompt("Please enter your name","");
    if (person!=null && person!="")
      {
        document.write(person);
      }
}

How can i do this??

Upvotes: 1

Views: 1371

Answers (4)

Joke_Sense10
Joke_Sense10

Reputation: 5402

Use Javascript Window.onload function which is fired when the entire page loads.

Javscript code:

 <script>
window.onload=function(){
 var person=prompt("Please enter your name","");
 if (person!=null && person!="")
  {
    $("#uname").append(person);
  }
}

</script>

HTML code:

<a href="#"  id="uname">Welcome</a>

Upvotes: 0

Nitu Bansal
Nitu Bansal

Reputation: 3856

try this

function inpt(){
var person=prompt("Please enter your name","");
if (person!=null && person!="")
  {
    $('.uname').html(person);
  }
 }

Upvotes: 0

Abhidev
Abhidev

Reputation: 7253

<a href="#" id="uname" class="uname">Welcome User!</a>

js

function inpt(){
    var person=prompt("Please enter your name","")
        uname = document.getElementById("uname");

    if (person!=null && person!="")
      {
        uname.innerHTML = 'Welcome '+person+ '!'
        //document.write(person);
      }
}

Upvotes: 5

Andreas Daoutis
Andreas Daoutis

Reputation: 1205

first in my opinion you should give the link an id. eg.

<a href="#" class="uname" id="uname">Welcome User!</a>

then instead of writing

document.write(person);

you should use

document.getElementById('uname').innerHTML = person;

hope that helps.

Upvotes: 0

Related Questions