Looping through multiple formfields with javascript?

I have a script where I can PregReplace something in a string with a defined value from my input field and it works like a charm.

echo '<input id="pfrom" type="text" value="" name="pfrom">';
function demoRegex(){
	var txt = document.newform.thestring.value;
	var re = new RegExp(document.newform.pfrom.value, "g");
	newtxt = txt.replace(re,'');
	alert(newtxt);
}

But what if I have multiple input field like this:

foreach($arr as $arr1=>$value){
	echo '<input id="pfrom" type="text" value="" name="pfrom[]">';
}

How do I get the javascript to run through all the fields?

Hoping for help… Thanks in advance :wink:

The following fiddle should do the job for you http://jsfiddle.net/chrisupjohn/Uj4wt/

You can also loop through the fields like an array:

function demoRegex(){		
	var frm = document.newform,
		txt = frm.thestring.value,
		frmArray = frm.pfrom,
		i;
	for( i = 0; i < frmArray.length; i++ ) {
		frmArray[i].value = txt;
	}
}