Reputation: 621
I have the following input fields in a form. I would like remove undefined and replace it with a blank space "". I can't seem to get it to work. I only would like to remove "undefined" not original value.
<input type="hidden" name="address1" id="address1" value="Some address undefined" class="address1">
<input type="hidden" name="address2" id="address2" value="Another address undefined" class="address1">
I would only like to remove undefined. So far I've tried:
var str = $('input').value().replace(/undefined/g, 'ada');
$('input').value(str);
and
$('input:contains("undefined")').each(function(){
$(this).html($(this).html().split("undefined").join(""));
});
Upvotes: 2
Views: 4436
Reputation: 781096
Use .val()
, not .value()
. You can give it a function as the argument. It will call this function with the old value as the argument, and the return value will be used as the new value (so you don't need to use .each()
as in the other answers).
$("#click").click(function() {
$("input").val(function(i, oldvalue) {
return oldvalue.replace(/undefined/g, '');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="address1" id="address1" value="Some address undefined" class="address1">
<input type="text" name="address2" id="address2" value="Another address undefined" class="address1">
<br>
<button id="click">Click to change</button>
Upvotes: 1
Reputation: 14434
You're using value()
, which doesn't exist in the jQuery API. Use val() instead. On top of this you should iterate through each input via, each()
and target the individual input of the current iteration using this
.
$('input').each(function() {
var str = $(this).val().replace(/undefined/g, 'ada');
$(this).val(str);
console.log(
$(this).val()
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="address1" id="address1" value="Some address undefined" class="address1" />
<input type="hidden" name="address2" id="address2" value="Another address undefined" class="address1" />
Upvotes: 1
Reputation: 68943
Use $('input').val()
not $('input').value()
with jQuery
's each()
.
$('input').each(function(i, el){
var str = $(el).val().replace(/undefined/g, '');
console.log(str);
$('input').val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="address1" id="address1" value="Some address undefined" class="address1">
<input type="hidden" name="address2" id="address2" value="Another address undefined" class="address1">
Upvotes: 1
Reputation: 11
Just use
$('input').val("");
instead of
var str = $('input').val().replace(/undefined/g, 'ada');
$('input').val(str);
Upvotes: 0
Reputation: 5363
You need to loop through each one of them using each
.
$('input').each(function(){
$this = $(this);
$this.val($this.val().replace(/undefined/g, ''))
console.log('new value is : ', $this.val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="address1" id="address1" value="Some address undefined" class="address1">
<input type="hidden" name="address2" id="address2" value="Another address undefined" class="address1">
Upvotes: 0