nokogiri and multiple css classes

453 views
Skip to first unread message

massi

unread,
Nov 19, 2009, 11:53:36 AM11/19/09
to nokogiri-talk
Hi,
How is it possible to select an HTML element that have more than one
css class ?
For example, how to select the p element based on it's class value
such as :
...
<p class='class1 class2'> ..... </p>
...

Thanks,

Massi

Aaron Patterson

unread,
Nov 20, 2009, 12:17:05 PM11/20/09
to nokogi...@googlegroups.com
Sure, just do as you would in normal CSS:

doc.css('p.class1.class2')

--
Aaron Patterson
http://tenderlovemaking.com/

massi

unread,
Nov 21, 2009, 12:16:51 PM11/21/09
to nokogiri-talk
Thank you Aaron !
Your suggestion is better than the solution I found (doc.xpath('//p
[contains(@class, "class1") and contains(@class, "class2")]') ).
The solution I found is not precise : p with classes such as class12
and class22 will be selected .... which is not good. However, with
your solution only elements with exact class names are selected.

Thanks again,

Massi


On Nov 20, 12:17 pm, Aaron Patterson <aaron.patter...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages