How to change the class of elements that have matching content

j1mmy

I have:

<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="foo">baz</div>
<div class="foo">qux</div>
<div class="foo">baz</div>
<div class="foo">qux</div>

And I would like all the elements that contain the same content as the one with a class of "bar" to also have a class of "bar" like so:

<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="bar">baz</div>
<div class="foo">qux</div>

But I can't just search for "baz" because that content is subject to change. Instead I have to figure out what's in the lone "bar" div then find the others that match it and change their class to "bar" too.

I've managed to get the content of the first "bar" element into a variable:

var = body.css(".bar").first.inner_text

But nothing I've tried from there as far as finding the matching content and replacing the class has worked.

DiodonHystrix

Be careful using body.css("div:contains('#{text_to_match}')").

input = '<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="foo">baza</div>
<div class="foo">qux</div>
<div class="foo">bazfdsfsdf</div>
<div class="foo">qux</div>'

The output will be:

<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="bar">baza</div>
<div class="foo">qux</div>
<div class="bar">bazfdsfsdf</div>
<div class="foo">qux</div>

If you want to match the exact same text in both elements, you should use: body.css("div[text() = '#{text_to_match}']")

input = '<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="foo">baza</div>
<div class="foo">qux</div>
<div class="foo">bazfdsfsdf</div>
<div class="foo">baz</div>'

The output would be:

<div class="bar">baz</div>
<div class="foo">qux</div>
<div class="foo">qux</div>
<div class="foo">baza</div>
<div class="foo">qux</div>
<div class="foo">bazfdsfsdf</div>
<div class="bar">baz</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I select elements that have no class?

How to select all elements that have (any) class

Change content of all elements having same class using javascript

Javascript: Change elements that have certain class and attribute -> pure Javascript

How to get matching elements from array and change array object?

How to change class of individual elements inside ngfor?

How to add a class to an element if all elements have a given class?

How to stop a class being added to an element if other elements have that class?

How to hide elements that have content using grid areas in CSS?

Toggle elements if they have a class

numpy change elements matching conditions

How can I compare 2 values from different elements and take action ONLY for elements that have matching values?

How can I have a variable (containing a class) change to another class?

How do you have a class change the value of an attribute of another class?

How to know how many class elements have been selected on click?

how to count how many elements have a certain class

How to change content on a page with iframes and have smooth dissolves?

How to change string content of a node that has child elements?

Change Class Content to href

how to scrape content using scrapy which have same class name

How to put the content of all elements with a certain class in a variable with Jquery

Change class of multiple elements

How to have a class which contains all the UI elements for a JFrame?

How to select all elements, which do NOT have a child with a certain class?

How to Ensure Elements have Loaded Before Adding Class on PageLoad

How to get a list of the ids of elements that have a certain class

How to select an id through a class when you have multiple elements in the same page that have same class

Find documents that have matching elements between arrays

How to change element's class depending on it's content?