in JavaScript by
How To Accessing Elements Using Javascript?

1 Answer

0 votes

To do something interesting with HTML elements, we must first be able to uniquely identify which element we want. In the example 


<form action="">

<input type="button" id="useless" name="mybutton" value="doNothing" />



We can use the "getElementById" method (which is generally preferred)

document.getElementById("useless").style.color = "red";

or we can use the older hierarchical navigation method,

document.forms[0] = "blue";

Notice that this uses the "name" attribute of the element to locate it.

# Example of Accessing Elements in a DOM.

<script type="text/javascript" >

function showStatus() {

var selectWidget = document.forms.beerForm.elements["beer"];

var myValue = selectWidget.options[selectWidget.selectedIndex].value;

alert('You drank a \"'+ myValue +"\"");

return true;



<form name="beerForm" action="">

<select name="beer">

<option selected="selected">Select Beer</option>


<option>Amstel Light</option>


<option>Corona Light</option>



<input type="button" name="submitbutton" value="Drink"

onclick="showStatus()" />


Related questions

0 votes
asked Jun 9 in JavaScript by sharadyadav1986
+1 vote
asked Dec 4, 2019 in JavaScript by SakshiSharma
0 votes
asked Apr 4, 2021 in Spring by Robindeniel