How to obtain a message from a textbox?

Hi experts, I have a button name "click me" I want to show the value of my textbox when I click this button. The value of my textbox is "Hello world" I want this to appear on the messagebox. Sorry, but i am a new beginner of HTML5. Tnx!
<html>
<body>
<form 
action="">
First name :<input type="text" 
name="iMsg" 
value="Hello world">

</form>

<button 
	> Click Me
</button>

</body>

</html>

Open in new window

Whing Dela CruzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
To get textbox value:

var message = document.getElementByName('iMsg').value;

Open in new window


or

var message = document.getElementsByName('iMsg')[0].value;

Open in new window


To display the message inside a message box, either create your own message box layout or simply use alert:

alert(message);

Open in new window

0
Whing Dela CruzAuthor Commented:
Hi Duy Pham, its working. But i need to add another text value to place into another line. My first text value is cow and my second text value is dog. I want to get this in different lines. Also I need to put a title on the message box.  
<script>
	function myFunction() 
	{
    		var x = document.getElementsByName("iMsg")[0].value + " first";
		alert(x);
		
	}
</script>

Open in new window

0
Duy PhamFreelance IT ConsultantCommented:
Using alert, you can display texts in different line by adding '\n' in between the texts. For example:

alert('Cow\nDog');

Open in new window


However you can't set title for default alert message box. You have to create your own alert function. See an example of Alertify here: http://fabien-d.github.io/alertify.js/.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Michel PlungjanIT ExpertCommented:
@Duy: document.getElementsByName('iMsg') only exists for array, hence only document.getElementByName('iMsg')[0].value; will work for the value of the first object named iMsg.

@Whing: Please when you paste more than one line of code, select it all and click on the code button to wrap it in
[ code ] tags
0
Duy PhamFreelance IT ConsultantCommented:
@Michel:  Thanks. You were right that getElementByName does not exists.
0
Whing Dela CruzAuthor Commented:
Hi experts, this is my code but it doesn't work please correct.
<script>
	function myFunction() 
	{
        var x = document.getElementsByName("animal")[0].value;
        var y = document.getElementsByName("animal")[1].value;
        alert('x + \n y');		
	}
</script>

Open in new window

0
Duy PhamFreelance IT ConsultantCommented:
Your alert line seems to be wrong. It should be:

alert(x + '\n' + y);

Open in new window


See example here: http://plnkr.co/edit/qiaIm1cVyCKp5n5Q446A?p=preview
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Thanks Duy Pham, its now working...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.