![]() Capt. Horatio T.P. Webb |
Parks -- Fall 2016 Version 2 -- 4/16/2018 |
Three ways to reference objects from javascript:
This model is used to get/set HTML form objects from javascript
In javascript, to access an HTML form object the format is:
document | . | form name OR forms(index) | . | element name OR elements(index) | . | value; |
<form name="form1"> <input type="text" name="text1" value="23"> <input type="button" value="go f1" onclick="f1()"> </form> |
function f1() { alert (document.form1.text1.value); } The four ways are: |
This model is used to get XML objects from javascript
In javascript, to access an XML node value the format is:
root | . | childNodes[index] | . | childNodes[index] | . | ... | . | childNodes[0].nodeValue; |
<form name="form2"> <input type="text" name="text2" value="<outer><branch1>23</branch1></outer>"> <input type="button" value="go f2" onclick="f2()"> </form> |
function f2() { data_value=document.form2.text2.value; parser=new DOMParser(); xmlDoc=parser.parseFromString(data_value,"text/xml"); root=xmlDoc.documentElement; alert (root.childNodes[0].childNodes[0].nodevalue); } |
This model is used to get/set any HTML objects's SS property or content from javascript
In javascript, to access an DOM value (typically style) the format is:
document | . | getElementById[ "id value" ] | . | style | . | CSS property | = | CSS property value; |
document | . | getElementById[ "id value" ] | . | innerHTML | = | value; |
<DIV id="div_id" style="color:red;">23</div> |
function f3() { alert (document.getElementById("div_id").innerHTML); document.getElementById("div_id").style.color="blue" } |
|