Reputation: 1029
I have the following html, and want to trim the forgot-password-email-address input value before submitting.
<form action="passwordpost/" method="post" id="forgot-password-form">
<label for="username">Email Address</label>
<input type="text" id="forgot-password-email-address" class="required" name="email" value=""><br/><br/>
<button type="submit" title="Find" class="form-button button forgotpasswordpost" name="submit" id="forgotpasswordpost-btn" style="float:right;">
<span><span>Send My Password</span></span>
</button>
</form>
The jQuery I have for this is below, yet it does not submit the form with the trimmed input. It submits, but with whitespace still in place.
$J("#forgot-password-form").submit(function(event){
$J("#forgot-password-email-address").val().trim();
return true;
});
Upvotes: 0
Views: 480
Reputation: 27022
val()
and trim()
both return strings, they don't modify strings. You need to reset the value:
var $field = $J("#forgot-password-email-address");
$field.val($field.val().trim());
Using the callback version, as undefined suggested, may be better here:
$J("#forgot-password-email-address").val(function(idx, value) {
return value.trim();
//return $.trim(value); // could also use jQuery's trim()
});
Upvotes: 4
Reputation: 3123
It is submitting the value in the textbox, update it's value
$J("#forgot-password-email-address").val($J("#forgot-password-email-address").val().trim());
You were not updating the value of the textbox previously.
Upvotes: 1
Reputation: 21475
Try this:
$J("#forgot-password-email-address").val($J("#forgot-password-email-address").val().trim());
Your code is only trimming the value of the input but not setting it to the element.
A small version of it:
var el = $J("#forgot-password-email-address");
el.val(el.val().trim());
Upvotes: 2