Mastering the document.getElementById() Method in JavaScript

Rumman Ansari   Software Engineer   2024-07-28 01:43:10   349  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

document.getElementById() method - Javascript

The document.getElementById() method returns the element of specified id.


html code

<span class="pln"> 
</span><span class="tag">&lt;form&gt;</span><span class="pln">  
    Enter No:</span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"number"</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"number"</span><span class="tag">/&gt;&lt;br/&gt;</span><span class="pln">  
    </span><span class="tag">&lt;input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"cube"</span><span class="pln"> </span><span class="atn">onclick</span><span class="pun">=</span><span class="atv">"</span><span class="pln">getcube</span><span class="pun">()</span><span class="atv">"</span><span class="tag">/&gt;</span><span class="pln">  
</span><span class="tag">&lt;/form&gt;</span><span class="pln">  </span>

Javascript Code

<span class="pln">
</span><span class="tag">&lt;script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">&gt;</span><span class="pln">  
</span><span class="kwd">function</span><span class="pln"> getcube</span><span class="pun">(){</span><span class="pln">  
    </span><span class="kwd">var</span><span class="pln"> number</span><span class="pun">=</span><span class="pln">document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"number"</span><span class="pun">).</span><span class="pln">value</span><span class="pun">;</span><span class="pln">  
    alert</span><span class="pun">(</span><span class="pln">number</span><span class="pun">*</span><span class="pln">number</span><span class="pun">*</span><span class="pln">number</span><span class="pun">);</span><span class="pln">  
</span><span class="pun">}</span><span class="pln">  
</span><span class="tag">&lt;/script&gt;</span><span class="pln">  </span>

Live Preview



No Questions Data Available.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.