I have a form with multiple text areas.
<textarea name="datasetname_1" cols="40" rows="5" id="datasetname_1" class="validate[required] text-input"></textarea><br>
<textarea name="datasetname_2" cols="40" rows="5" id="datasetname_2" class="validate[required] text-input"></textarea><br>
<textarea name="datasetname_3" cols="40" rows="5" id="datasetname_3" class="validate[required] text-input"></textarea><br>
I would like to replace any text that contains commas with dashes. This should work for text that’s typed directly in the text areas or text that is pasted over. The text should be replaced before the form is submitted. How can I accommplish this with jquery or javascript?
I have tried this
$("#formID").submit(function() {
$("textarea").each(function() {
($(this).val().replace(',','-'));
});
});
with no luck.
will fetch the value, replace one comma with a hyphen and then does nothing because you don’t store the value into a variable.
Instead, you can use:
$(this).val('some value');to actually set the value.E.g.: http://jsfiddle.net/pimvdb/HsFbN/.
It is arguable whether jQuery should be used for everything, but you can also omit it here: