Choy Roman
Choy Roman

Reputation: 7

Focus Div after button Click

<input type="button" onclick="$('#c')[0].focus()" value="test focus"/>
<div>
    <div id="c" tabindex="1" style="margin-top:800px;">testing</div>
</div>

Why this Html Code won't Work . what i want is after the click it will direct to focus div. Thank in advance :)

Upvotes: 0

Views: 3896

Answers (2)

Pawan Thakur
Pawan Thakur

Reputation: 591

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script>
$(document).ready(function(){

$("#mybutton").click(function(){

    // focus and select

   $('#c').focus().select();

})
})
</script>

// Give id to button

<input type="button" id="mybutton" value="test focus" />
<div>
  <div id="c" tabindex="1" style="margin-top:300px;">testing</div>
</div>

Upvotes: 0

unalignedmemoryaccess
unalignedmemoryaccess

Reputation: 7441

It works as it is expected. When you click button, your div is in focus.

#c:focus {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" onclick="$('#c')[0].focus()" value="test focus" />
<div>
  <div id="c" tabindex="1" style="margin-top:300px;">testing</div>
</div>

On button click, testing div has border.

Upvotes: 1

Related Questions