0

I wrote function, which control input text. When I click button, in Input set value. In that time I need that function 'StartDate1.change' control change input value. So if this is value no equals 12/24/2013 -> clear field. But this is function doesn't work. Can you help me fix it?

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>

        <form action="demo_form.asp">
            Date: <input type="text" name="Phone" title="title1" value="">
            <input id="target" class="ms-ButtonHeightWidth" type="button"  accesskey="O" 
                   value="Add date" >
        </form>

        <script type="text/javascript">
            $(document).ready(function(){
                $( "#target" ).click(function() {
                    var StartDate=$('[title="title1"]');              
                    StartDate.val('12/25/2013');
                });

                var StartDate1=$('[title="title1"]');
                StartDate1.change(function(event){//doesn't get here
                    window.setInterval(function(){
                        if($(event.target).val()==='12/24/2013')
                        {
                            //some code
                        }else
                        {
                            $(event.target).val('');
                        }
                    });
                });


            });

        </script>

    </body>
</html>

This is example on fidler:

Fiddle

1
  • Pass 'delay' to window.setInterval and check when the callback to clear the text is called. Check this fiddle jsfiddle.net/yPYDJ/2 Commented Dec 25, 2013 at 8:34

3 Answers 3

1

If you need to control value in input field use keyup handler

http://jsfiddle.net/yPYDJ/5/

Sign up to request clarification or add additional context in comments.

1 Comment

It's not work. Because some times input was enable. And only button can change text. That's why your code not work for me.
0

The onchange event only fires when the value changes and the input loses focus.

You can trigger it manually changing your code like this:

$( "#target" ).click(function() {
  var StartDate=$('[title="title1"]');              
  StartDate.val('12/25/2013');
  StartDate.change();
});

1 Comment

No . I can't run this way, because button can't change onclick code.
0

The function passed to window.setInterval is called repeatedly. So value set by the function on button click is reset immediately.

var timeFunction;                 
$( "#targetb" ).click(function() {
                    var StartDate=$('[title="title1"]'); 
                    StartDate.off('change');
                    StartDate.val('12/25/2013');
                    StartDate.on('change',function(event){
                    console.log("lol");
                    timeFunction=window.setInterval(clearInput,2000);
                });

                });

              function clearInput(){
                        var StartDate=$('[title="title1"]'); 
                        if(StartDate.val()==='12/24/2013')
                        {
                            //some code
                        }else
                        {
                            StartDate.val('');
                            window.clearInterval(timeFunction);
                        }

                    }

I have created a new fiddle http://jsfiddle.net/yPYDJ/4/. Check this and accept it ifthis is what you expected.

5 Comments

You are 12 seconds early I was about to click submit button :(
@dholakiyaankit I wish you to be earliest next time :) Hope you have validated my answer.
Ya obviously @ahamedmustafam
I can't modify $( "#targetb" ).click(function().
@user3132839 , you dont have to modify it. Add the new event handler. It will be called in sequence. jsfiddle.net/u9uaa

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.