I have an HTML form where I need to do the following
- when value of Select Box changes to initial "Not Selected" value (where values of the option is 0), I am to highlight the select box, when it's non-0, restore the original coloring of the select
- when I submit the form, and the Select box value is 0, do not submit the form, and highlight the box
Doing all of this left me with code duplication. See highlight
and restore
How do I get rid of the duplication? Can I?
highlight("kit_model");
$('#kit_submit').prop("disabled", true);
$("#kit_model").change(function() {
if ($("#kit_model").val() == 0)
{
highlight("kit_model");
$('#kit_submit').prop("disabled", true);
}
else
{
restore("kit_model");
$('#kit_submit').prop("disabled", false);
}
});
$("#custom_kit_form").submit(function() {
if ($("#kit_model").val() == 0)
{
highlight("kit_model");
return false;
}
$.post(
"web.php
$("#custom_kit_form").serialize(),
$("#main").load('main.php')
);
return false;
});
Form
<form action="" id="custom_kit_form" method="post" name="custom_kit_form">
<table>
<tr>
<td><label for="kit_model" id="label_kit_model">FS Kit</label></td>
<td><select id="kit_model" name="kit_model">
<option value="0">-- SELECT --</option>
<option value="573">TUBING</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input id="kit_submit" name="kit_submit" type="submit" value="Save"></td>
</tr>
</table>
</form>