n00b0101
asked on
replace all instances of text using jquery
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:
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').eac h(function () {
$(this).find('*').replace( new RegExp(val, "gi"), array[val]));
$(this).find('*').replace( new RegExp(val, "gi"), array[val]));
});
};
The above yields:
I also tried:
and various other iterations... Can anyone point me in the right direction?
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>
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').eac
$(this).find('*').replace(
$(this).find('*').replace(
});
};
The above yields:
missing ; before statement
[Break On This Error] $(this).find('*').replace(new RegExp(val, "gi"), array[val]));
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]));
}
});
and various other iterations... Can anyone point me in the right direction?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, I missed the underscore two renaming.
Ccheck this:
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');
});
That RegExp does rename this:
<input name="section[1][item][62]" id="section-1-item-1" value="Enter Section 1 / Item 1" />
To this:
<input name="section_2[item][62]" id="section-2-item-1" value="Enter Section 1 / Item 1" />
ASKER
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);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No....
I have this:
And want this:
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>
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
neither of these give me what I need... thanks anyway.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
none of these worked for me, but i appreciate the help
ok... why did you close the question? Why did these not work for you?
ASKER
I can't get the regexp to search for the section+character+clonecou
So, if:
Open in new window
I'm trying to search replace:
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...