PDA

View Full Version : How to search for elements with class starts with particular character and replace th



sarasa
24 Sep 2013, 5:04 AM
For example,



<div class="level1"></div>
<div class="level1 level3"></div>I need to search for elements with class name starting with 'level' and programatically replace them as "level2". How can this be achieved in extjs?

existdissolve
24 Sep 2013, 5:59 AM
For example,



<div class="level1"></div>
<div class="level1 level3"></div>I need to search for elements with class name starting with 'level' and programatically replace them as "level2". How can this be achieved in extjs?

You have two separate issues, really. The first is to find all the elements that match your initial criteria...e.g., has a class name that begins with "level".

This is rather trivial with Ext JS:


Ext.select('[class^=level]')

This will only give you back the matching elements, however, not the classes. So in order to handle the instances where one matching element has multiple matching class names that need to be replaced, you'll need to check each class on the element and replace as needed.

A quick (and definitely not refined or thoroughly tested solution might look like this:


Ext.select('[class^=level]').each(function(el, c, index) {
var classlist = el.dom.className.split(' ');
for (var i = 0; i < classlist.length; i++) {
var theclass = classlist[i];
if (theclass.search('^level') != -1) {
el.removeCls(theclass);
el.addCls('level2');
}
}
});

Here's a fiddle that you can play with:

l6

sarasa
25 Sep 2013, 5:27 AM
Thanks.. Got the idea :)