Monday, June 16, 2008

To change the label value dynamically through javascript

We can change the label value dynamically through javascript as follows:

Trick is to make the span as an elementobject with an id

<SCRIPT language=JavaScript>
<!-- Begin
function checkrequired() {

var fields = 0
var i = 0
var empty = true
var fields = document.form1.length
var textvalue = ""

for (i = 0; i < fields ; i++) {
switch (document.form1.elements[i].type){
case "select-one" :
break;
case "select-multiple" :
if (document.form1.elements[i].selectedIndex != -1){
empty = false
}
break;
case "checkbox" :
if ( document.form1.elements[i].checked) {
empty = false
}
break;
default :
break;
}
}

if (!empty) {
document.form1.submit();
} else {
document.getElementById("errortext").innerHTML='Select one or more item';
return false;

}
}
// End -->
</SCRIPT>

<form name=form1 method="POST" action="check.asp">

<SPAN id=errortext>You must check at least one of the available domain
names below to continue.</SPAN><br>

<input type="checkbox" name="k1" value="astim" class="input">
<input type="checkbox" name="k2" value="whax" class="input">
<input onclick="return checkrequired();" type="image" SRC="ok.gif" border="0" name="submitit" >
</form>

No comments: