0

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?

2
  • I'm from the C# stack. There might be also something like a DataFormatString. Commented Dec 6, 2012 at 7:27
  • I tried but didn't remove that part. No errors even. Commented Dec 6, 2012 at 7:28

5 Answers 5

2

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];
});
Sign up to request clarification or add additional context in comments.

Comments

1

This should do the trick

$('input.check').each( function() {
    var elem = $(this);
    var value = elem.val().split(' ')[0];
    elem.val(value);
});

3 Comments

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? ;-)
I used this. working. but finally I used T.J. Crowder's answer.
@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 <3
0

Dont have idea about your server side code, you might do like this

$(document).ready(function () {
    $('.check').each(function () {

            $(this).val($(this).val.replace(" 00:00:00",""));

    }) });

Comments

0

Jquery/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

-1
$('.check').each(function(i){
  var checkValue= $(this).val();
  checkValue.replace('00:00:00','');
  $(this).val(checkValue);
});

Try this code

3 Comments

...and actually setting the value of the field?
You've fixed half of the bugs reported above.
which part of the bug is remaining

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.