Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get id of focused element using javascript

I have single Html page having many anchor elements. This single Html page contains many div parts. Only one part displays at time and a anchor specific to this part will focused. this html also contain a div to open popup. When this popup will open anchor specific to this will focused. Now I want get the anchor focused earlier to popup anchor focused. My code is following:-

<body onload="Main.onLoad();" onunload="Main.onUnload();">

        <a href='javascript:void(0);' id='anchor' onkeydown='Main.MainKeyHandler();'></a>
        <a href='javascript:void(0);' id='anchorAboutUs' onkeydown='Main.MainKeyHandler1();'></a>
        <a href='javascript:void(0);' id='anchorSecondScreen' onkeydown='Main.MainKeyHandler2();'></a>
        <a href='javascript:void(0);' id='popupanchor' onkeydown='PopupKeyHandler();'></a>
        <div id="ParentDiv">
            <div id="SplashScreenDiv">
                <div id="SplashScreen"></div>
            </div>
            <div id="Logo"></div>
            <div id="Header"></div>
            <div  id='messagePopup'  style='position:relative;width:500px;z-index:1000;background-color:#ffffff;'><div ></div ><div id='popupReturn'>Return</div><div>
            <div id="PopUp">
                <div id="YesBtn"></div>
                <div id="NoBtn"></div>
            </div>
            <div id="FirstScreen">
            <div id="testinfo" style="color:#ff0000; width:600px; height:50px; overflow:hidden; font-size:xx-large; "></div>
                <div id="btn_horoscope"></div>
                <div id="btn_aboutus"></div>
            </div>
            <div id="AboutUsScreen" >
                <div id="AboutUsImgDiv">
                    <div id="AboutUsImg"></div>
                </div>
                <div id="aboutUsInfoDiv">
                    <div id="aboutUsInfo"></div>
                </div>
            </div>
            <div id="SecondScreen" >
                <div id="HoroscopeImg" class="HoroImage"></div>
                <div id="SelSunSignMsgDiv">
                    <div id="SelSunSignMsg">Select your SunSign </div>
                </div>
                <div id="SelSunSignDiv">
                    <div id="SelSunSign">
                    <div id="SunSign_0"></div>
                    <div id="SunSign_1"></div>
                    <div id="SunSign_2"></div>
                    <div id="SunSign_3"></div>
                    <div id="SunSign_4"></div>
                    <div id="SunSign_5"></div>
                    <div id="SunSign_6"></div>
                    <div id="SunSign_7"></div>
                    <div id="SunSign_8"></div>
                    <div id="SunSign_9"></div>
                    <div id="SunSign_10"></div>
                    <div id="SunSign_11"></div>
                    </div>
                </div>

            </div>
        </div>


    </body>

any help will appriciated

like image 503
Rajanikant Shukla Avatar asked Apr 03 '12 11:04

Rajanikant Shukla


People also ask

How do you find the focus element?

It can be used to get the currently focused element in the document: Syntax: var ele = document. activeElement; Return value: It returns the currently focused element in the document.

How do you check if an element is focused in JS?

hasFocus() The hasFocus() method of the Document interface returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.

What is focus () JavaScript?

focus() Javascript focus() methods helps to highlight a HTML form element. It sets the element as an active element in the current document. In current documentation, focus can be applied to only one single element. The focus can be applied either to a text, a button, etc.

How do you find the ID of an element?

getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string.


1 Answers

You need document.activeElement.id. You can find more information here, which has been criticized before.

like image 184
Aragon Avatar answered Oct 16 '22 08:10

Aragon