HTML DOM Button autofocus प्रॉपर्टी <button> एलिमेंट के autofocus प्रॉपर्टी से जुड़ी है। button autofocus प्रॉपर्टी का उपयोग यह निर्दिष्ट करने के लिए किया जाता है कि HTML दस्तावेज़ पर एक बटन को पेज लोड होने पर फोकस मिलना चाहिए या नहीं।
सिंटैक्स
निम्नलिखिक सिंटैक्स है -
button autofocus प्रॉपर्टी सेट करना -
buttonObject.autofocus = true|false
यहां, true|false निर्दिष्ट करता है कि दिया गया इनपुट बटन को पेज लोड होने पर फोकस मिलना चाहिए या नहीं।
- True − इनपुट बटन को फोकस मिलता है
- False − इनपुट बटन को फोकस नहीं मिलता।
उदाहरण
आइए HTML DOM button autofocus प्रॉपर्टी का एक उदाहरण देखें -
<!DOCTYPE html> <html> <body> <button type="button" id="MyButton" autofocus>BUTTON</button> <p>Click the below button to know if the input button above automatically gets the focus on page load or not</p> <button onclick="buttonFocus()">CLICK IT</button> <p id="Sample"></p> <script> function buttonFocus() { var x = document.getElementById("MyButton").autofocus; if(x==true) document.getElementById("Sample").innerHTML="The input button does get focus on page load"; else document.getElementById("Sample").innerHTML="The input button does not get focus on page load"; } </script> </body> </html>
आउटपुट
यह निम्नलिखिक आउटपुट उत्पन्न करेगा -

CLICK IT बटन पर क्लिक करने पर -

उपरोक्त उदाहरण में -
हमारे पास id “MyButton” वाला एक बटन है और autofocus प्रॉपर्टी सक्षम है -
<button type="button" id="MyButton" autofocus>BUTTON</button>
फिर हमारे पास buttonFocus() फ़ंक्शन को निष्पादित करने के लिए CLICK IT बटन है -
<button onclick="buttonFocus()">CLICK IT</button>
buttonFocus() फ़ंक्शन getElementById() मेथड का उपयोग करके बटन एलिमेंट को प्राप्त करता है और इसका autofocus मान प्राप्त करता है, जो बूलियन है और इसे वेरिएबल x को असाइन करता है। कंडीशनल स्टेटमेंट्स का उपयोग करके हम जांचते हैं कि autofocus मान true है या false और इससे जुड़े id “Sample” वाले <p> एलिमेंट में उपयुक्त टेक्स्ट प्रदर्शित करते हैं।
function buttonFocus() { var x = document.getElementById("MyButton").autofocus; if(x==true) document.getElementById("Sample").innerHTML="The input button does get focus on page load"; else document.getElementById("Sample").innerHTML="The input button does not get focus on page load"; }