Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How to Accessing HTML Elements using javascript?
How to Accessing HTML Elements using javascript?
✍: Guest
To do something interesting with HTML elements, we must first be able to uniquely identify which element we want.
In the example below:
<body>
<form action="">
<input type="button" id="useless" name="mybutton" value="doNothing" />
</form>
</body>
We can use the "getElementById" method using the "id" attribute to identify the element (which is generally preferred)
document.getElementById("useless").style.color = "red";
or we can use the "name" attribute to identify the element:
document.forms[0].mybutton.style.color = "blue";
Here is another example of accessing elements with the DOM object:
<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;
}
</script>
<form name="beerForm" action="">
<select name="beer">
<option selected="selected">Select Beer</option>
<option>Heineken</option>
<option>Amstel Light</option>
<option>Corona</option>
<option>Corona Light</option>
<option>Tecate</option>
</select>
<input type="button" name="submitbutton" value="Drink"
onclick="showStatus()" />
</form>
2010-11-23, 3949👍, 0💬
Popular Posts:
How Do You Uninstall JUnit Uninstalling JUnit is easy. Just remember these: Delete the directory tha...
What Is a LABEL Tag/Element? - XHTML 1.0 Tutorials - Understanding Forms and Input Fields A "label" ...
What is the benefit of using an enum rather than a #define constant? The use of an enumeration const...
How To Write a Query with a Right Outer Join? - Oracle DBA FAQ - Understanding SQL SELECT Query Stat...
Where Do You Download JUnit? Where do I download JUnit? I don't think anyone will ask this question ...