Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Working with DOM Element

View previous topic View next topic Go down 
AuthorMessage
kimcrowley
Member
Member
kimcrowley

Posts : 2
Join date : 2011-04-18

Working with DOM Element Empty
PostSubject: Working with DOM Element   Working with DOM Element EmptyWed May 11, 2011 10:06 pm

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?
Back to top Go down
Skilletrockz
Super Moderator
Super Moderator
Skilletrockz

Posts : 45
Join date : 2011-05-11

Working with DOM Element Empty
PostSubject: Re: Working with DOM Element   Working with DOM Element EmptyWed May 11, 2011 10:07 pm

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');
Back to top Go down
kimcrowley
Member
Member
kimcrowley

Posts : 2
Join date : 2011-04-18

Working with DOM Element Empty
PostSubject: Re: Working with DOM Element   Working with DOM Element EmptyWed May 11, 2011 10:08 pm

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.
Back to top Go down
Skilletrockz
Super Moderator
Super Moderator
Skilletrockz

Posts : 45
Join date : 2011-05-11

Working with DOM Element Empty
PostSubject: Re: Working with DOM Element   Working with DOM Element EmptyWed May 11, 2011 10:09 pm

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.
Back to top Go down
Sponsored content




Working with DOM Element Empty
PostSubject: Re: Working with DOM Element   Working with DOM Element Empty

Back to top Go down
 

Working with DOM Element

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Array element display, newbie question
» Enhanceviews.net not working right now?
» Windows 7 ....not working...
» working web flood
» Very useful simple coding tip - how to save time working with lists

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Working with DOM Element Edit-trash Useless :: Trash-