triplethreat77
triplethreat77

Reputation: 1296

Change closest input val?

I have an instance where my datepickers won't work by button click, so I figure find the closest input and .click() or .focus() (for it's datepicker, since the same field is to be affected). For the sake of this demo, from the button/icon click - find the closest input and focus/click on it. If correct, the border will glow blue. http://jsfiddle.net/81tvr0op/

HTML

<table>
    <tr>
        <th>
            <div class="input-group date">
                <input type="text" />
                <span class="input-group-btn">
                    <button class="btn default" type="button">
                        <i class="fa fa-calendar"></i>
                        Icon
                    </button>
                </span>
            </div>
        </th>    
    </tr>
</table>

jQuery

...something like

$(".date > span > button").on('click', function (e) {
                e.preventDefault();
                $(this).closest("th").find("input:text").click();
            });

...right?

Upvotes: 0

Views: 112

Answers (2)

Elliot B.
Elliot B.

Reputation: 17661

If I understand correctly, you are trying to place the cursor into the input box when the user clicks the button.

To do that, you should use .focus() instead of .click().

Working example: http://jsfiddle.net/81tvr0op/1/

Upvotes: 2

KJ Price
KJ Price

Reputation: 5964

From your jsfiddle, something like this should work.

$("button").on('click', function (e) {
    e.preventDefault();
    $(this).parent().siblings(0).focus();
});

Or, give your input an id and do something like:

$("button").on('click', function (e) {
    e.preventDefault();
    $("#dateInput").focus();
});

Upvotes: 0

Related Questions