Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    72
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Looping through formfields?

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

    Code:
    <?php
    echo '<input id="pfrom" type="text" value="" name="pfrom">';
    ?>
    <script>
    function demoRegex(){
    	var txt = document.newform.thestring.value;
    	var re = new RegExp(document.newform.pfrom.value, "g");
    	newtxt = txt.replace(re,'');
    	alert(newtxt);
    }
    </script>
    But what if I have multiple input field like this:
    PHP Code:
    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 ;-)

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    Firstly get all the fields named "pfrom[]". Then loop through them as needed.
    Code:
    <script>
    function demoRegex(val){
    	var txt = document.newform.thestring.value;
    	var re = new RegExp(val, "g");
    	newtxt = txt.replace(re,'');
    	alert(newtxt);
    }
    
    function foo() {
        var pfrom = document.getElementsByName("pfrom[]");
        for (var i = 0, len = pfrom.length; i < len; i++) {
            demoRegex(pfrom[i].value);
        }
    }
    </script>
    To answer more specifically one needs to know whether you want to apply all N regular expressions to the same initial string and get N resulting strings, or you wish to pass this initial string through all the RE sequentially and get a single resulting string.
    Last edited by Amphiluke; 01-07-2012 at 12:05 PM.
    I am still learning English

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    72
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    To answer more specifically one needs to know whether you want to apply all N regular expressions to the same initial string and get N resulting strings, or you wish to pass this initial string through all the RE sequentially and get a single resulting string.
    Well I want to get a single string.

  • #4
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    In that case the script may look like this:
    Code:
    <script>
    function demoRegex(){
    	var pfrom = document.getElementsByName("pfrom[]"),
    	    txt = document.newform.thestring.value,
    	    re;
    	for (var i = 0, len = pfrom.length; i < len; i++) {
    		re = new RegExp(pfrom[i].value, "g");
    		txt = txt.replace(re,'');
    	}
    	alert(txt);
    }
    </script>
    Last edited by Amphiluke; 01-07-2012 at 12:17 PM. Reason: drop unnecessary formal argument
    I am still learning English

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    72
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Amphiluke View Post
    In that case the script may look like this:
    Code:
    <script>
    function demoRegex(){
    	var pfrom = document.getElementsByName("pfrom[]"),
    	    txt = document.newform.thestring.value,
    	    re;
    	for (var i = 0, len = pfrom.length; i < len; i++) {
    		re = new RegExp(pfrom[i].value, "g");
    		txt = txt.replace(re,'');
    	}
    	alert(txt);
    }
    </script>
    Oh yes.... Thanks alot... Your the champ ;-)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •