?
Solved

replace all instances of text using jquery

Posted on 2011-10-11
14
Medium Priority
?
268 Views
Last Modified: 2012-08-14
I'm trying to replace all instances of a particular string and have tried a number of ways to do it, but more often than not, I get an error saying replace isn't a function...

For example, in the following HTML:

<div id="section-1-master">
  <span class="section_1-span">Title</span>
  <form id="section-1-formadd">
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
 </form>
</div>

Open in new window


I want to replace all instances of:

section-1 with section-2
section_1 with section_2
section[1] with section_2

I've tried many variations of essentially the same thing, but here is the most recent attempt:  


        var cloneUp = 2;
      var cloneCount = 1;
 
        var array = { };
            array["section-"   + cloneCount] = "section-"  + cloneUp;
            array["section_"   + cloneCount] = "section_"  + cloneUp;
            array["section\\[" + cloneCount] = "section\[" + cloneUp;

      for(var val in array) {
            $('#section-1-master').each(function() {
                  $(this).find('*').replace(new RegExp(val, "gi"), array[val]));
                  $(this).find('*').replace(new RegExp(val, "gi"), array[val]));
            });
      };

The above yields:
missing ; before statement
[Break On This Error] $(this).find('*').replace(new RegExp(val, "gi"), array[val])); 

Open in new window


I also tried:
$('#section-1-master').find('*').each(function() {
		for(var val in array) {
			$(this).replace(new RegExp(val, "gi"), array[val]));
			$(this).replace(new RegExp(val, "gi"), array[val]));
		}
	});

Open in new window


and various other iterations...  Can anyone point me in the right direction?
0
Comment
Question by:n00b0101
  • 6
  • 5
  • 3
14 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 999 total points
ID: 36952805
For that replacement you need only ONE RegEx but you do not replace Strings; yo want to rename IDs and Names!
That would not go easily...

The RegExp for your rename is:

$('#section-1-master').find('*').each(function() {
			$(this).id=$(this).id.replace(/section[-_\[]1\]?/,'section-2');
			$(this).name=$(this).name.replace(/section[-_\[]1\]?/,'section-2');
	});

Open in new window

0
 

Author Comment

by:n00b0101
ID: 36952842
This doesn't work for me... It replaces everything with section-2, when it should replace it with section+thecharacter+cloneUp

I can't get the regexp to search for the section+character+clonecount and replace it with section+character+cloneup...

So, if:

cloneCount = 1
cloneUp = 2

Open in new window


I'm trying to search replace:

'section-'+cloneCount with 'section-'+cloneUp
'section_'+cloneCount with 'section_'+cloneUp
'section['+cloneCount with 'section['+cloneUp

Open in new window


But, everything I've tried results in a missing : error, a replace is not a function error, or a $(this) is undefined error...
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 999 total points
ID: 36952866
My RegExp renames this:
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />

Open in new window

to this:
<input name="section-2[item][62]" id="section-2-item-1" value="Enter Section 1 / Item 1" />

Open in new window

Show what you expect.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 63

Expert Comment

by:Zvonko
ID: 36952886
Sorry, I missed the underscore two renaming.

Ccheck this:
$('#section-1-master').find('*').each(function() {
			$(this).id=$(this).id.replace(/section-1/,'section-2').replace(/section[_\[]1\]?/,'section_2');
			$(this).name=$(this).name.replace(/section-1/,'section-2').replace(/section[_\[]1\]?/,'section_2');
	});

Open in new window

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36952890
That RegExp does rename this:
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />

Open in new window

To this:
<input name="section_2[item][62]" id="section-2-item-1" value="Enter Section 1 / Item 1" />

Open in new window

0
 

Author Comment

by:n00b0101
ID: 36952961
Doesn't work for me...

function createNewSection() {

	var cloneUp = 2;
  
  	var newSection = $('#section-1').clone(function() {
  			$(this).find('*').each(function() {
				$(this).id=$(this).id.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
				$(this).name=$(this).name.replace(/section-1/,'section-'+cloneUp).replace(/section[_\[]1\]?/,'section_'+cloneUp);
			});
	});
			
	$('#sections').append(newSection);

}

Open in new window

0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 999 total points
ID: 36952971
So you have this:
<div id="section-1-master">
  <span class="section_1-span">Title</span>
  <form id="section-1-formadd">
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
 </form>
</div>

Open in new window

And you want this:
<div id="section-1-master">
  <span class="section_1-span">Title</span>
  <form id="section-1-formadd">
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
 </form>
</div>
<div id="section-2-master">
  <span class="section_2-span">Title</span>
  <form id="section-2-formadd">
   <input name="section_2[item][62]" id="section-2-item-1" value="Enter Section 1 / Item 1" />
 </form>
</div>

Open in new window

That is NOT renaming ids but cloning one <form> to two <form><form>s???
0
 

Author Comment

by:n00b0101
ID: 36953350
No....

I have this:

<form>
<div id="section-1-master">
  <span class="section_1-span">Title</span>
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
</div>
</form>

Open in new window


And want this:

<form>

<div id="section-1-master">
  <span class="section_1-span">Title</span>
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
</div>

<div id="section-2-master">
  <span class="section_2-span">Title</span>
   <input name="section[2][item][62]" id="section-2-item-1" value="Enter Section 2 / Item 2" />
</div>

</form>

Open in new window

0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 501 total points
ID: 36953439
It was just the way you were accessing the attributes in the javascript

see here: http://jsfiddle.net/rjurd/dJrMs/
var cloneUp = 2;

function createNewSection() {

    var newSection = $('#section-1-master').clone();
    newSection.attr('id','section-' + cloneUp + 'master');
    newSection.find('span').removeClass();
    newSection.find('span').addClass('section_' + cloneUp + '-span');
    newSection.find('form').attr('id','section-' + cloneUp + '-formadd');
    newSection.find('input').eq(0).attr('id','section-' + cloneUp + '-item-1');
    newSection.find('input').eq(0).attr('name','section[' + cloneUp + '][item][62]');
    newSection.find('input').eq(0).attr('value','Enter Section ' + cloneUp + ' / Item 1');
    cloneUp++;

    $('#sections').append(newSection);

}

Open in new window

0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 999 total points
ID: 36954043
Here my method:
<html>
<head>
<title>Zvonko &#42;</title>
<script>
var strFirstFormSection;
function cloneFormSection(cloneUpNum){
  var theForm = document.forms[0];
  var theText = theForm.innerHTML;
  if(!strFirstFormSection){
    strFirstFormSection = theText;
  }
  theText = strFirstFormSection.replace(/section-1/g,'section-'+cloneUpNum);
  theText = theText.replace(/section[_\[]1\]?/g,'section_'+cloneUpNum);
  theText = theText.replace(/\bSection 1\b/g,'Section '+cloneUpNum);
  theForm.innerHTML+=theText;
}

window.onload=function(){
  for(var i=2;i<9;i++){
    cloneFormSection(i);
  }
}
</script>
</head>
<body>
<form>
<div id="section-1-master">
  <span class="section_1-span">Title for Section 1</span>
   <input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
</div>
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:n00b0101
ID: 36962149
neither of these give me what I need... thanks anyway.
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 501 total points
ID: 36965721
What are you after then?  I based my method on your last post...
0
 

Author Closing Comment

by:n00b0101
ID: 36980152
none of these worked for me, but i appreciate the help
0
 
LVL 43

Expert Comment

by:Rob
ID: 36983130
ok... why did you close the question?  Why did these not work for you?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question