I have several text boxes with class name 'check'. It already has '12/12/2001 00:00:00' string. I want to remove the '00:00:00' part from every text boxes. How do I do this from jquery?
5 Answers
Just get the value and truncate it at the first space.
$("input.check").each(function() {
var index = this.value.indexOf(" ");
if (index >= 0) {
this.value = this.value.substring(0, index);
}
});
You can do it with a shorter bit of code using split, but it works harder:
$("input.check").each(function() {
this.value = this.value.split(' ')[0];
});
Comments
This should do the trick
$('input.check').each( function() {
var elem = $(this);
var value = elem.val().split(' ')[0];
elem.val(value);
});
3 Comments
T.J. Crowder
There's exactly zero reason to wrap up the DOM element in a jQuery wrapper (
value is completely reliable on input elements), but if you're going to, at least only do it once? ;-)Namal
I used this. working. but finally I used T.J. Crowder's answer.
maček
@T.J.Crowder, thanks. I was originally writing it as a one-liner; by the time it started looking too long, I just split it into two lines and overlooked the two
$(this) expressions. Cheers <3Jquery/Javascript
var val = $(".check").val();
$(".check").val(val.substring(1, val.indexOf("00:00:00")-1));
HTML
<input class="check" value="12/12/2001 00:00:00"/>
Example: http://jsfiddle.net/VkcFm/
Comments
$('.check').each(function(i){
var checkValue= $(this).val();
checkValue.replace('00:00:00','');
$(this).val(checkValue);
});
Try this code
3 Comments
maček
...and actually setting the value of the field?
T.J. Crowder
You've fixed half of the bugs reported above.
Jignesh Parmar
which part of the bug is remaining
DataFormatString.