Search code examples
javascriptjqueryhtmlfocusshow-hide

Setting focus to anchor inside div after show?


I am trying to set the focus to an anchor element inside a hidden div element, while clicking an another anchor link. I have tried couple of options like .focus(), .focusin(). But nothing is working.

My Code:

    <script>
    $(document).ready(function(){
        $("#clickShow").on('click', function(){
            $("#showDescription").show();
            $("#showDescription").find("a").focus();
        });
    });
</script>
<div>
    <a href="#" id="clickShow">View Hidden Text</a>
</div>

<div id="showDescription" style="display:none;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap <a href="#">Link Text</a> into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

I wanted to know that, What I am missing here to get it done?

Appreciate your esteemed help.


Solution

  • Actually its working! Used jquery 2.1.1

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#clickShow").on('click', function(){
                $("#showDescription").show();
                $("#showDescription").find("a").focus();
            });
        });
    </script>
    <div>
        <a href="#" id="clickShow">View Hidden Text</a>
    </div>
    
    <div id="showDescription" style="display:none;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap <a href="http://www.google.com">Link Text</a> into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

    EDIT: Tried in 1.11.1 too... its working... Perhaps your issue is something else