Introduction to Javascript - 1

Q: Write a javascript program that inputs your name and displays it in the browser.

<script>
var n;
n=prompt("Enter your name","Type Your Name");
document.write(n);
</script>




Q:
Write javascript code that inputs first and second name from the user. It displays the first name in the first line and second name in the second line and full name in the third line.

<script>
var f, s, full;
f = prompt("Enter your first name","Your First Name");
s = prompt("Enter your second name","Your Second Name");
full = f + " " + s;
document.write("<h3>String Example: </h3>");
document.write ("Your First name is  "+ f);
document.write("Your Second Name is : " + s);
document.write("<br>");
document.write("Your full name is: " + full);
</script>

Javascript Object Models:

Q: Write Javascript code that displays a button on the page. A message should appear when the user click the button. 

<!DOCTYPE html>
<html>
<body>
<script>
function msg(){
  alert("Event Handling with buttons");
 }
</script>

<form name='f'>
<input type="button" value="Click here to display message" onClick="msg();">
</form>

</body>
</html>

Q: Write a Javascript code to display three text boxes. If the user changes the contents of first two text boxes; the script combines the contents of the first two text boxes and displays them in third text box.

<!DOCTYPE html>
<html>

<body>
<script>
function msg(){
  frm.txtFullName.value = frm.txtFirstName.value + " " + frm.txtLastName.value;
 }
 
</script>

<form name="frm">
<p>First Name: </p>
<input type="text" name="txtFirstName" Size="15" onChange="msg();">
<p>Last Name: </p>
<input type="text" name="txtLastName" Size="15" onChange="msg();">
<br><p>Full Name: </p>
<input type="text" name="txtFullName" Size="30">
</form>

</body>
</html>








Comments