5

I have a Bootstrap modal with a form that is triggered when two buttons on the page are clicked.

When one specific button is clicked, I want a specific input field within the form to be in focus, as in the cursor is in that field and the styling for :focus is applied.

$('.bio-input').focus(); does not seem to work (although it works in the code snippet). Even though this element I'm trying to bring into focus is in a modal, the modal is rendered on page load as a partial, so the element is available in the DOM on click.

At the bottom of my view (slim), I have the partial that holds the modal:

 = render partial: 'users/profile/edit_profile_modal'

The modal has an id of #popup-editprofile.

Then, below, my button tag has that id as the data-target, and the data-toggle is 'modal'.

Thanks in advance for your help.

$(document).ready(function() {
  $('.edit-bio-btn').click(function() {
    $("#bio-input").focus();
  })
});
#bio-input:focus {
  border-left: 5px solid orange;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button name="button" type="submit" class="button btn btn-xs edit-bio-btn" data-target="#popup-editprofile" data-toggle="modal"><i class="fa fa-pencil"></i><span>Edit bio</span></button>

<textarea name="profile[bio]" id="bio-input" placeholder="Enter Bio" class="form-control edit-profile-input"></textarea>

14
  • 5
    $(".bio-input").focus(); Commented Apr 16, 2015 at 18:36
  • Why do you want to add the :focus selector via jQuery? Why not just put it in your CSS? Commented Apr 16, 2015 at 18:37
  • 1
    Yep sorry! Maybe because of the modal needing to render first? Ya, element need to be available in DOM prior to be focused. So you'd have better to post relevant code regarding this modal, surely it has a event to bind once it is opened Commented Apr 16, 2015 at 19:01
  • 1
    @ahimmelstoss - Sounds to me like the modal copies the HTML of the partial and displays it in a modal, meaning that there's a duplicate set of elements that are in the partial in the DOM. What we really need to see is the code that is used to display the modal. Commented Apr 16, 2015 at 19:16
  • 1
    @ahimmelstoss - I meant the JavaScript that displays the modal, not what renders the HTML onto the page in the first place. Are you using a library to display the modal? Commented Apr 16, 2015 at 22:48

3 Answers 3

3

You can use the focus() function. Once the button is clicked, the input field will be focused.

$('.edit-bio-btn').click(function() {
    $('input.thisClass').focus();
})

DEMO

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

3 Comments

This works for me in a demo/snippet, but not in the context of my application. I wonder if the modal has something to do with it?
Do you have more than one element with the class 'bio-input'?
@entropic nope, just doubled checked.
2

Bootstrap Modal Events: http://getbootstrap.com/javascript/#modals-events

After you finally provided the information that you were using a bootstrap modal, the answer becomes clear:

$('#id-of-modal-element').on('shown.bs.modal',function() {
  $('.bio-input',this).focus();
});

This snippet says, when your modal element fires the shown.bs.modal event, focus on the .bio-input element that is inside of that modal.

If this doesn't work for you, then you've got to provide a link to your page, or code that reproduces your issue.

1 Comment

Awesome, that did it. Didn't consider the modal being a part of it. Thanks!
2

Try this: http://jsfiddle.net/mgm1k5s7/

$("#button").on("click", function () {
    $("#input").focus();
});

Comments

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.