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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Location
    Sunshine Coast; Australia
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy JS Search Array for substring then edit entry

    Hi Guys,

    I am trying to make a function that will search for a substring, in this case 'http://instagr.am/p/' within a text area on a form then add '[embed]' & '[/embed]' tags around it.

    I have currently got the following code

    Code:
    <script type='text/javascript'> 
    	function show_alert() {
    		str = formname.elements['inputid'].value;
    		arr = (str.split(' ') + '<br />'); 
    		jQuery.each(arr, function() {
    			if (arr.indexOf('http://instagr.am/p/') >= 0) {
    				alert('An http://instagr.am/p/ link has been found');
    				//alert('It is entry ... in the array');
    				//Then edit the entry 
    			}
    		}
    		);
    	} 
    </script>
    This breaks the contents of the textarea (at spaces) into an array then finds any entries with 'http://instagr.am/p/' in them (could be 'http://instagr.am/p/29fdghHdv').

    Once it has found any and all of these entries it will add an '[embed]' code to the beginning and an '[/embed]' tag at the end.

    Example...

    http://instagr.am/p/vuHdeyfa2
    is converted to:
    '[embed]http://instagr.am/p/vuHdeyfa2[/embed]'


    Then the changes must reflected in the textarea input

    "formname.elements["textareaid"].value = editedstring;


    Thankyou for your assistance.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    What is the problem?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    Does this help?

    Code:
    <textarea id = "mytext" rows = "6" cols = "40" >Lorem veggies sunt bona vobis http://instagr.am/p/vuHdeyfa2 proinde vos postulo esse magis http://instagr.am/p/vuxx  burdock groundnut salad pepper </textarea>
    
    <script type = "text/javascript">
    
    var txt = document.getElementById("mytext").value;
    var arr = txt.split(" ");
    for (var i = 0; i<arr.length; i++) {
    if (arr[i].indexOf('http://instagr.am/p/') >= 0) {  // found
    var str = arr[i];
    var newstr = "[embed]" + str + "[/embed]";
    changeVal2("mytext",str,newstr);
    }
    }
    
    function changeVal2(IDS, passedV, change2){
    var text = document.getElementById(IDS).value; 
    var match = new RegExp(passedV, 'gi');	// global and case-insensitive
    var output = text.replace(match, change2);      // replacing variable string with variable replacement value
    document.getElementById(IDS).value = output;
    }
    
    </script>
    We were lucky that a more serious incident wasn't averted. Presenter, Channel 4
    Last edited by Philip M; 03-26-2012 at 01:55 PM. Reason: Improved

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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