Reputation: 749
I'm trying to empty a textarea that is populated from a database with php.
Here's the code:
<select name="facility" id="facility">
<option></option>
<?php
global $__CMS_CONN__;
$sqlqry = "SELECT * FROM facility_db";
$stmt = $__CMS_CONN__->prepare($sqlqry);
$stmt->execute();
while($row = $stmt->fetchObject())
{
echo "<option value=\"$row->id\">$row->facility</option>";
}
?>
</select>
<button type="button" onclick="addFacility()">Add</button><button type="button" onclick="reset()">Reset</button>
<textarea readonly="readonly" id="facilities" name="facilities" rows="10" cols="50" value="<?php echo $facilitylist; ?>" ><?php echo $facilitylist; ?></textarea>
<input type="text" id="facilityids" name="facilityids" value="<?php echo $facilities; ?>"/>
The select box is populated from the database, and the Add button adds the selected facility to the textarea, and the id to the hidden field for database storage.
Up to this point everything works beautifully. The reset button will clear both the textarea and the hidden field and start over. The problem arises whan a user has saved their profile and had some facilities selected. The reset button only works on additions, but won't clear anything that was pulled from the database.
The javascript for the reset is just setting the value of those fields id to empty.
function reset()
{
document.getElementById('facilities').value = null;
document.getElementById('facilityids').value = null;
}
But it doesn't work on the hidden field either. I set it to text for testing and even when i manually delete what's in the textbox, if I click the reset button it comes back.
What is going on?
Upvotes: 1
Views: 2201
Reputation: 17169
function reset(){
document.getElementById('facilities').value= '';
document.getElementById('facilityids').value = '';
}
for textarea might have to do innerHTML = '';
as dr molle says the default reset method might have been used over your reset()
Upvotes: 1
Reputation: 117314
Give the function another name. There is a predefined method reset() inside forms, that function restores the initial values of form-fields.
A button is a form-field, by this the reset()-method of his form is invoked instead of your custom method.
Upvotes: 2