0

I have a small form and When I click the Join button, and enter my information, I want it to save the value of the Username form, and then replace the Join and Signup button with their name or "Hello" + name.

So I have a JSFiddle Here: http://jsfiddle.net/LCBradley3k/t3HF5/ I would think it would be something like the following code, but that doesn't work:

var name  = document.getElementById("name");

Then I would want it to put name in some type of div that replaces the two button at the top.

2 Answers 2

1

If you're using jQuery (which your jsfiddle indicates you are), this should be fairly trivial.

var name = $('#name').val();

Hope that helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Now where exactly would this go in the JSFiddle? Also, how would I add the value to a div, so I could position it, etc...?
Not sure what you need it for exactly, but I would use it to grab the inputted value by the user whenever they click the buton $('#join').on('click', function(). And what do you mean by add the value to a div? If you want to add a value to something, do $('#name').val('here's the value'), which in this case is not advisable since it is a user input field.
Reread your post again. It looks like once you have the name field, target the ID of the div and place the content there. $('#divID').html(name); for instance.
Also take a look at .serialize especially if you're working with forms with lots of fields it makes life much easier when collecting values and serializing them ready for posting: api.jquery.com/serialize
1

Using pure javascript (no jQuery), if it's an input element, to get the input value use:

var name=document.getElementById("name").value;

To replace a div's content with name:

document.getElementById("div_id").innerHTML=name;

You can also use html:

document.getElementById("div_id").innerHTML="<p>" + name + "</p>";

Or replace the div's value:

document.getElementById("div_id").value=name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.