Jump to content

[SOLVED] Adding text to page using javascript + input box


idire

Recommended Posts

Right, not sure if this is possible.

 

If i asked a user to enter a username into an input box, could javascript take that username (without submiting the form) and add that username to a piece of text undernearth

 

i.e. they enter their username and:

 

@email.co.uk -> username@email.co.uk

 

 

Thanks

Link to comment
Share on other sites

oh course...something like this?

 

<script type="text/javascript">
  function addText ( ) {
    var text = document.getElementById('my_input').value;
    var ele = document.getElementById('my_text');
    ele.innerHTML += text + '<br />';
  }
</script>
<input type="text" id="my_input" /> <input type="button" value="Add" onclick="addText();" />
<div id="my_text"></div>

Link to comment
Share on other sites

yup...code is even shorter...you can use onkeyup...just make sure you set the innerHTML instead of appending it:

 

<input type="text" onkeyup="document.getElementById('my_text').innerHTML = this.value;" />
<div id="my_text"></div>

 

 

Link to comment
Share on other sites

Thanks it works, just one other thing:

 

how do I make it so the div doesnt caus the text before it and after it to go on a new line?

 

This will send a unique link by email to: <div id="username">yourusername</div>@email.com

 

becomes:

 

This will send a unique link by email to:

yourusername

@email.com

 

 

Thanks for the help :)

 

Worked it out myself :) Changed <div> to <span>

Thanks for the help

 

Link to comment
Share on other sites

  • 4 months later...

Hello, I'm new face in here...

Found this topic via Google, and this helped me kind of lot...

 

I've kind of code where should add new fields by clicking button, for sending more information to php-script.

<div id="areas">
  <p>Tiles between: <input name="minValues[]" type="text" id="min" size="5" /> - <input name="maxValues[]" type="text" id="max" size="5" /></p>
</div>

Using script below for onClick-action

function addRow() {
var elem = document.getElementById('areas');
var newText = '<p>\nTiles between:\n<input name="minValues[]" type="text" id="min" size="5" /> - <input name="maxValues[]" type="text" id="max" size="5" />\n</p>\n';
elem.innerHTML += newText;  
}

 

I've little problem, that if I've already entered some values to old fields, and add a new one,

all click will clear all older fields values also.

 

How might I could avoid that?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.