I am trying to use jQuery to do a simple thing: clear the input fields of a group of input field within a div whenever the form loads, or if the user changes a picklist; but I’m having a devil of a time getting the selector to work.
First, here’s my onclick and onload triggers:
<form name="EditGenotype" action="process_GenotypeMaint.php" method="POST" onsubmit="return false" onload=clear_fetch() >
and on the picklists:
<SELECT multiple size=6 NAME="marker" onchange=show_marker() onclick=clear_fetch() >
Next, here’s my HTML input elements in the div I want to clear:
<div class=fetch_results>
<fieldset>
<LEGEND><b>Edit Genotype, call 1</b></LEGEND>
<boldlabel>Allele_1</boldlabel><input id=allele_1_1 name=allele_1_1 size=5 >
<boldlabel>Allele_2</boldlabel><input id=allele_1_2 name=allele_1_2 size=5 >
<boldlabel>Run date</boldlabel><input id=run_date1 name=run_date1 size=10 disabled=true>
</fieldset>
<fieldset>
<LEGEND><b>Edit Genotype, call 2</b></LEGEND>
<boldlabel>Allele_1</boldlabel><input id=allele_2_1 name=allele_2_1 size=5 >
<boldlabel>Allele_2</boldlabel><input id=allele_2_2 name=allele_2_2 size=5 >
<boldlabel>Run date</boldlabel><input id=run_date2 name=run_date2 size=10 disabled=true>
</fieldset>
<fieldset>
<LEGEND><b>Edit Genotype, call 3</b></LEGEND>
<boldlabel>Allele_1</boldlabel><input id=allele_3_1 name=allele_3_1 size=5 >
<boldlabel>Allele_2</boldlabel><input id=allele_3_2 name=allele_3_2 size=5 >
<boldlabel>Run date</boldlabel><input id=run_date3 name=run_date3 size=10 disabled=true>
</fieldset>
</div>
Finally, here’s my script:
function clear_fetch() {
$('#fetch_results:input', $(this)).each(function(index) {
this.value = "";
})
}
However, nothing happens…so, I must not have the selector correct. Does anybody see what I’ve done wrong?
Fiddle: http://jsfiddle.net/simple/BdQvp/
You can do it like so:
I have added two buttons in the Fiddle to illustrate how you can insert or clear values in those input fields through buttons. You just capture the onClick event and call the function.
Update:
Check out this great answer below by Beena as well for a more universal approach.