6

I have a document ready block as follows:

$(document).ready(function () {
    $('#addTagLink').click(function () {
        $('#addTagField').show();
        $('#addTagField').val("");
        $('#addTagField').focus();
    });
});

The addTagField is a regular text input that has display:none set by css on page load.

When a user clicks on the addTagLink element the input field is shown correctly but focus doesn't get set to the field as intended.

I figured it must be something to do with the display:none / show() functionality, so changed the $('#addTagField').focus(); to another field $('#name').focus();which worked perfectly.

Can anyone suggest firstly why I see this issue and secondly, how to fix it?

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
DaFoot
  • 1,475
  • 8
  • 29
  • 58

2 Answers2

6

Turns out it was a problem with my element IDs. Oops.

DaFoot
  • 1,475
  • 8
  • 29
  • 58
6
// Variable to store element
var btn = document.querySelector( ".btn-primary");

// Click Event
$(btn).click(function() {

  // Jquery to define which element will get focus
  $(".form-control").focus();

});
Mohammad Ayoub Khan
  • 2,422
  • 19
  • 24
  • 2
    While this code may answer the question, it is better to include the essential parts of the answer here and provide the code for reference. – Busti Jul 19 '18 at 16:14