user1575201
user1575201

Reputation: 1

jQuery - How would I replace

Hopefully this is a quick question as I know everyone is busy and I appreciate any advice I can get at this moment.

I'm trying to replace a proceeding class name with a preceding nested class name using jQuery. My first issue is that the class I am trying to capture (class2random_number) is nested within "class1" while the proceeding one isn't (class3).

The second issue is that the nested class name (class2random_number) nested in "class1" has a predetermined class name but ends with a random number. I would like to lookup and capture the name of class2random_number with the random number intact and replace "class3" with "class2random_number".

For example the following lines:

<p class="class1"><span class="class2random_number">content_stays_the_same</span></p>
some content goes here
<span class="class3">content_stays_the_same</span>

I'm sorry if this looks and sounds complicated but it is. At least for me. If anyone can give me any advice that would be fantastic. My jQuery skills are very novice but I'm trying to grasp as much as I can. Thank you for your time.

Upvotes: 0

Views: 63

Answers (1)

Ram
Ram

Reputation: 144689

Something like this?

var cls = $('.class1 span').attr('class').replace('class2', "")
$('.class3').attr('class', cls)

http://jsfiddle.net/ZRze3/1/

Upvotes: 1

Related Questions