vendredi 27 mars 2015

How do I capture data entered in field of html form using javascript

I have developed a html contact form and I am trying to understand how to apply javascript to be able to capture the inputs/values of each field. I believe you need to use document.getelementById() but how do I apply it and do I need to use an event such as onBlur to capture it when a user leaves the field, radio button or checkbox?


Thanks


HTML



<!DOCTYPE html>
<html>
<head>
<title>Contact Me</title>
<link rel="stylesheet" type="text/css" href="contactform_Lab8.css">
</head>

<body>

<form id="contactus">
<fieldset>
<label for="name">Name:</label>
<input id="name" type="text" name="name" autofocus required>
<label for="email">Email:</label>
<input id="email" type="email" name="email" required>
<label for="phone">Phone:</label>
<input id="phone" type="tel" name="phone" required>
<label for="status">Status:
<select id="status" name="status" required>
<option value="client">Client</option>
<option value="partner">Partner</option>
<option value="vendor">Vendor</option>
</select>
</label>
<label for="subscribe">
<input id="subscribe" type="checkbox" name="subscribe" value="check" checked>
Send me your newsletter</label>
<label for="sales">
<label for="support">
<input id="slsSupport" type="radio" name="slsSupport" value="sales" checked>Sales
<input id="slsSupport" type="radio" name="slsSupport" value="support">Support
</label>
</label>
<label for="msg">Message:</label>
<textarea id="msg" name="msg" rows="10" cols="30" required></textarea>
</fieldset>
<fieldset>
<button type="submit">Send</button>
<button type="reset">Reset</button>
</fieldset>
</form>
<script type="contactform_Lab8.js"></script>

</body>

</html>


CSS



/*Borders of fields for validation and indication*/
input:invalid{
box-shadow: 0 0 2px .5px red;
}
textarea:invalid{
box-shadow: 0 0 2px .5px red;
}
/*Spacing around fields this is in place of <br>*/
label{display: block; padding-top: 5px}

Aucun commentaire:

Enregistrer un commentaire