PDA

View Full Version : how to write the function setAll() correctly??


wanye
09-15-2002, 09:21 AM
Hi all, i am kinda stuck here...i am trying to write a function setaAll() that can update all values on the form when i click on the "Set All" button, but theres seems something wrong ..Can anyone help me to see wads wrong wif it..? thx

below here is the form as shown :

<html>
<head>
<script>

var xstring = "<root><author></author><title></title><subtitle></subtitle><topic></topic><content></content><intro></intro><summary></summary></root>"

// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.loadXML(xstring)

function getAuthor(){return getVal("author")}
function getTitle(){return getVal("title")}
function getSubTitle(){return getVal("subtitle")}
function getTopic(){return getVal("topic")}
function getContent(){return getVal("content")}
function getIntro(){return getVal("intro")}
function getSummary(){return getVal("summary")}




function getVal(n){
return xml.documentElement.getElementsByTagName(n).item(0).text;
}

function setAuthor(v){setVal("author",v)}
function setTitle(v){setVal("title",v)}
function setSubTitle(v){setVal("subtitle",v)}
function setTopic(v){setVal("topic",v)}
function setContent(v){setVal("content",v)}
function setIntro(v){setVal("intro",v)}
function setSummary(v){setVal("summary",v)}

function setAll(v)
{
setAuthor(v)
setTitle(v)
setSubTitle(v)
setTopic(v)
setContent(v)
setIntro(v)
setSummary(v)

}

function setVal(n,v){
xml.documentElement.getElementsByTagName(n).item(0).text = v;
}

function showXML(){
alert(xml.xml)
}

function clearForm()
{

}

</script>
<head>

<body>
<form>
<input type=text name=t0><br>
<input type=button value="Set Author " onclick="setAuthor(this.form.t0.value)">
<input type=button value="Get Author " onclick="alert(getAuthor())"><br><br>

<input type=text name=t1><br>
<input type=button value="Set Title" onclick="setTitle(this.form.t1.value)">
<input type=button value="Get Title" onclick="alert(getTitle())"><br><br>

<input type=text name=t2><br>
<input type=button value=" Set Sub Title " onclick="setSubTitle(this.form.t2.value)">
<input type=button value=" Get Sub Title " onclick="alert(getSubTitle())"><br><br>

<input type=text name=t3><br>
<input type=button value=" Set Topic " onclick="setTopic(this.form.t3.value)">
<input type=button value=" Get Topic " onclick="alert(getTopic())"><br><br>

<input type=text name=t4><br>
<input type=button value=" Set Content " onclick="setContent(this.form.t4.value)">
<input type=button value=" Get Content " onclick="alert(getContent())"><br><br>

<input type=text name=t5><br>
<input type=button value=" Set Intro " onclick="setIntro(this.form.t5.value)">
<input type=button value=" Get Intro " onclick="alert(getIntro())"><br><br>

<textarea name=t6 rows=10 cols=50></textarea><br>
<input type=button value=" Set Summary " onclick="setSummary(this.form.t6.value)">
<input type=button value=" Get Summary " onclick="alert(getSummary())"><br><br>
<br>
<input type=button value="Show XML " onclick="showXML()"><br>
<input type=button value="Set All " onclick="setAll(this.form)"><br>
<input type=reset value=Clear Form onclick="clearForm(this.form)">

</form>
</body>

</html>

ShriekForth
09-16-2002, 03:47 PM
This is just a problem with references, one you are trying to do a set with an object, the other you are using the value.

The setAll function, you are passing it a form, but then trying to set each item with just he form object, it needs to be more specific. There are a couple of ways to do this, make each set know the form name/value to set, or pass the value to it, like so.

function setAll(v){
setAuthor(v.t0.value)
setTitle(v.t1.value)
setSubTitle(v.t2.value)
setTopic(v.t3.value)
setContent(v.t4.value)
setIntro(v.t5.value)
setSummary(v.t6.value)
}

In that case, you would not have to modify each set, like setTitle, or setIntro on each button.

ShriekForth