Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle focus onclick of a button

Tags:

People also ask

What does focus() do?

JavaScript | Focus() It sets the element as the active element in the current document. It can be applied to one html element at a single time in a current document. The element can either be a button or a text field or a window etc. It is supported by all the browsers.


I have a form inside a panel. The panel opens and closes when a button is clicked. When the panel is opened [i.e., with the click of the button], I want to focus on the first input text box in the form which is inside the panel. When I close the panel [i.e., again clicking on the same button], I want to loose the focus from that input box.

So, in simple words, I want to toggle the focus on the text input box when the onclick event happens on the button.

I gave the input text box an id and did the following:

<input type="text" id="code" placeholder="enter code">

Inside the script tag:

$('.button-element').click(function(){ $('#code').focus();});

This makes the focus on the input text box but I want to remove the focus when I click the button again.

Please help me with this.

Thank you.