Examples

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

<!DOCTYPE html> <html> <body> <form id="form1"> <button id="button1" type="button">Click me!

</button> </form> <p>The id of the form containing the button is: <script> document.write(document.getElementById("button1").form.id); </script></p> </body> </html>

The id of the form containing the button is: form1

<!DOCTYPE html> <html> <body> <form id="frm1" action="form_action.asp"> First name: <input type="text" name="fname" value="Donald"><br> Last name: <input type="text" name="lname" value="Duck"><br> <input type="submit" value="Submit"> </form> <p>The number of elements in "frm1" are: <script> document.write(document.getElementById("frm1").length); </script> </p> </body> </html>

The number of elements in "frm1" are: 3

<!DOCTYPE html> <html> <head> <script> function myFunction() { var x=document.getElementById("fname"); x.value=x.value.toUpperCase(); } </script> </head> <body> Enter your name: <input type="text" id="fname" onblur="myFunction()">

<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
</body> </html>

<!DOCTYPE html> <html> <head> <script> function loadImage() { alert("Image is loaded"); } </script> </head> <body> <img src="w3javascript.gif" onload="loadImage()" width="100" height="132"> </body> </html>

<!DOCTYPE html> <html> <head> <script> function getElements() { var x=document.getElementsByTagName("input"); alert(x.length); } </script> </head> <body> <input type="text" size="20"><br> <input type="text" size="20"><br> <input type="text" size="20"><br><br> <input type="button" onclick="getElements()" value="How many input elements?"> </body> </html>

<!DOCTYPE html> <html> <body> <input type="button" id="button1" value="Click Me!">

<p>The text on the button is: <script> document.write(document.getElementById("button1").value); </script></p> </body> </html> The text on the button is: Click Me!

<!DOCTYPE html> <html> <body> <form> Buttons: <input type="button" id="firstbtn" value="OK"> <input type="button" id="secondbtn" value="OK"> </form> <p>Click the button below to disable the first button above.</p> <button onclick="disableElement()">Disable button</button> <script> function disableElement() { document.getElementById("firstbtn").disabled=true; } </script> </body> </html>

<!DOCTYPE html> <html> <head> <script> function formReset() { document.getElementById("frm1").reset(); } </script> </head> <body> <p>Enter some text in the fields below, then press the "Reset form" button to reset the form.</p> <form id="frm1"> First name: <input type="text" name="fname"><br> Last name: <input type="text" name="lname"><br><br> <input type="button" onclick="formReset()" value="Reset form"> </form> </body> </html>

You might also like