mercredi 6 mai 2015

How to target the focused input dynamically

I have a Jquery code that will pop-up with a box saying "Today" only when a user focuses on an input field. When the user clicks on the box, this will populate the targeted input field with the current date. Currently my code is working but the problem is as follows:

Problem: When someone clicks on the Today button, it populates all the input fields on the page instead of the input being focused on.

I created a JSFiddle to give you the best representation of my site's structure and will give you an idea of what's happening. Im still learning a lot in with Jquery lately but this has stumped me. I tried to target the input itself by using $("input[id*=Next_Update]").val(now); but that just breaks the code.

Here is a quick sample of my code with the JSfiddle JSFiddle: http://ift.tt/1Jrktmo

$(document).ready(function() {
    $(".im_whiteboard [id*=Next_Update]").on('focusin', function() {
        var div = $('#now');
        var top = $('input').offset().top + $('input').height() + 5;
        var left = $('input').offset().left;
        $(div).css('top', top);
        $(div).css('left', left);
        $(div).fadeIn();
    });
    $('input').on('focusout', function() {
        $('#now').fadeOut();
    });
    $('#now').on('click', function() {
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();

        if(dd<10) {
            dd='0'+dd
        } 

        if(mm<10) {
            mm='0'+mm
        } 

        today = mm+'/'+dd+'/'+yyyy;
       $('input').val(today); 
    });
});

I would appreciate any support on this.

Aucun commentaire :

Enregistrer un commentaire