Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2011, 10:04 AM   PM User | #1
mamamia
New Coder

 
Join Date: Apr 2007
Posts: 34
Thanks: 1
Thanked 1 Time in 1 Post
mamamia is an unknown quantity at this point
Working with DOM Element

I'm trying to understand how I can call a jQuery method on a specific element only. For example, let's say I have:

Code:
<div class="foo">test</div>
I clone this using:

Code:
var fooClone = $('.foo').clone();
And now, i want to call the jQuery replaceAll() method to replace the "foo" class with "foobar", but only on the cloned element (fooClone). The idea is that once the class name is replaced, I will then insert it into the DOM using append(). So what is the jQuery syntax to denote that I want the replaceAll() method to use this variable/element instead of looking at the whole DOM?

Thanks in advance.

--mamamia
mamamia is offline   Reply With Quote
Old 05-11-2011, 10:24 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
You can work with the clones as with regular DOM nodes before you insert them. Now, I understand that you want to change the class of the clones from “foo” to “foobar”? Don’t know why you need replaceAll() for that as toggleClass() (or removeClass and addClass) would do but you can chain the functions as usual:

Code:
var fooClone = $('.foo').clone().toggleClass('foo foobar');
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 05-11-2011, 05:15 PM   PM User | #3
mamamia
New Coder

 
Join Date: Apr 2007
Posts: 34
Thanks: 1
Thanked 1 Time in 1 Post
mamamia is an unknown quantity at this point
The problem is that I want to replace different classes within the clone, where each class has a different name, but a similar pattern. Specifically, the children of the clone have class names such as "text-1", "image-1", "container-1". My intention is to change all of those class names to "text-2", "image-2", "container-2", accordingly. I was planning on using the replaceAll() method since I can use the "-1" as the string to search for to replace.

Any help would be greatly appreciated.
mamamia is offline   Reply With Quote
Old 05-11-2011, 05:56 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,614
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Ah, I see what you’re getting at. I had a similar application lately where I had to clone form fields (and labels) and I’ve gone with a simple regular expression. I’m gonna paste what I’ve used, maybe this will give you a starting point.
Code:
// the variable “item” has been defined earlier in the script and is a wrapper element
// that contained the elements whose values had to be replaced –
// it’s the root of the clone(s)
var ind = item.index();
var clone = item.clone().hide();
clone.find('input[type=text]').val('');
clone.find('input[type=text],label[for]').each(function() {
	if($(this).attr('id').length) {
		var num = $(this).attr('id');
		var att = 'id';
	}
	else if($(this).attr('for').length) {
		var num = $(this).attr('for');
		var att = 'for';
	}
	var newNum = num.replace(/[0-9]/,ind+1);
	$(this).attr(att,newNum);
Basically I had IDs and for attributes in the same style as your classes which I had to number sequentially. Don’t know if replaceAll() is a better way; this has been my first idea to solve the problem.
__________________
Don’t click this link!

Last edited by VIPStephan; 05-11-2011 at 05:59 PM..
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
mamamia (05-12-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:52 PM.


Advertisement
Log in to turn off these ads.