0

Script replaces rel attributes to class.

Full code - http://jsbin.com/efozi3/8/

It doesn't work for links, where used more than 2 values in rel.

An example - the first item:

<a class="s1" rel="t1 t2 t3" href="#">One</a>

First three <li> should become blue, but now only the first one does.

 <li class="t1">
 <strong>1</strong>
 </li>
 <li class="t2">
 <strong>2</strong>
 </li>
 <li class="t3">
 <strong>3</strong>
 </li>

This line doesn't work as expected (supports maximum 2 values in rel):

return $('.' + elem.rel.replace(' ', ', .'));

You can edit code directly on http://jsbin.com/efozi3/8/edit/

Thanks.

asked Jul 13, 2010 at 16:03
2
  • 1
    (Why do people keep abusing rel for this kind of thing? What did that poor attribute do to deserve it?) Commented Jul 13, 2010 at 16:11
  • @bobince: To validate, perhaps Commented Jul 13, 2010 at 16:20

1 Answer 1

5

Believe it or not, Javascript's replace function only replaces the first occurrence of the search text.
To replace every occurrence, you need to pass a regex with the g (Global) flag.

Change it to

return $('.' + elem.rel.replace(/\s+/g, ', .'));
answered Jul 13, 2010 at 16:06
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry; I forgot about this quirk in my previous answer.
I guess there's no other way (except looping until string remains unchanged). Doesn't seem like cheating to me...
@Koen: That's not what he meant (I believe). I gave him his current code

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.