...">

How to use jQuery to focus on a specific div tag?

Let's say I got some DIV tags in an HTML page, for example:

<DIV ID="all">
    <DIV ID="div1">              
    <Input type="radio" name="data['site1']['user']" VALUE="1">user1<br/>
    </DIV>
    <DIV ID="div2">              
    <Input type="radio" name="data['site1']['user']" VALUE="2">user2<br/>
    </DIV>
    <DIV ID="div3">              
    <Input type="radio" name="data['site1']['user']" VALUE="3">user3<br/>
    </DIV>
    <DIV ID="div4">              
    <Input type="radio" name="data['site1']['user']" VALUE="4">user4<br/>
    </DIV>
</DIV>

Is it possible to make the same effect in jQuery as follows:

Document.getElementByID('div2').focus();
+3
source share
3 answers

Equivalent:

$("#div2").focus();

but you can only focus on the form element:

$("#div2 :input").focus();

will focus on the first form element inside the element with div2 id.

+3
source

You cannot set focus on items div, only the following items are โ€œcustomizable":

  • A
  • AREA
  • LABEL
  • INPUT
  • SELECT
  • TEXTAREA
  • BUTTON

, div, , , , ...

+3

You cannot focus on the div. But if you want to focus on the switch in div2, you can try:

$('#div2 input').focus();
+2
source

Source: https://habr.com/ru/post/1745737/


All Articles