• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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

0
Whing Dela Cruz
Asked:
Whing Dela Cruz
  • 4
  • 3
4 Solutions
 
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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
 
Whing Dela CruzAuthor Commented:
Thanks Duy Pham, its now working...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now