Accessing all from elements :Javascript

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
Hi,

I am trying to access all form elements using this JS code

Code:
function srchvalidate(theform)
{
var str = '';
var elem = document.getElementById('theform').elements;
for(var i = 0; i < elem.length; i++)
{
if(elem[i].value=="")
{
alert("You have not filled all parameters");
}
else
{
theform.submit.type="submit";
theform.submit();
}
}
}

I call the function here :
Code:
<input type="button" id="Button2" name="submit" value="Search" style="position:absolute;left:80px;top:219px;width:75px;height:24px;z-index:11" tabindex="5" onclick="srchvalidate(document.frmsearch)">

the code does not give any msg as required...there are four comboboxes in the form "frmsearch"
thanks
 

ASPX.King

New Member
Messages
155
Reaction score
0
Points
0
have you turned on debugging in your browser, assuming you're using IE7 or IE8?
 

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
i m using ie 6
it gives error "could not convert undefined to object "

i used opera now it popped the error console


Edit:

Is it wrong to change the type of button at runtime ??i change changed the type of button to submit when the condition changes to false ...
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
I think it's wrong to change the type of an element at runtime...
but the good news is it's not even required!

I'm getting the required result in both firefox and ie with this code:
Code:
function srchvalidate(theform) {
	var elements = form.childNodes;
	for(i = 0; i<elements.length; i++) {
		element = elements.item(i);
		if(element.nodeName == "INPUT") {
			if(element.value == "") {
				alert("You have not filled in all parameters.");
				return;
			}
		}
	}
	theform.submit();
}

I hope this is of any use
 

VPmase

New Member
Messages
914
Reaction score
0
Points
0
In marshain's code in the first if/then statement he uses "form" instead of "theform". That could be the problem. But then there is always the choice to do something like:

HTML:
<form name="testform">
<input type="textbox" name="testin" />
<input type="button" value="Test" onclick="testcheck();" />
</form>

<script>
function testcheck(){
var f = document.testform;
if(f.testin.length > 0){
alert("Longer than 0");
} else {
alert("0....");
}
}
</script>
 
Last edited:

anuj_web

New Member
Messages
145
Reaction score
0
Points
0
but how to use this code for a combo...i need to check for a combo i already mentioned
 
Top