views:

30

answers:

1

Can someone please explain how to add a custom attribute to an HTML tag using Ruby with Hpricot gem?

I have a tag that looks like this:

<div class="test" id="tag1" style="">

and I want to add a custom integer attribute called 'Readable=0' so it looks like this:

<div class="test" id="tag1" style="" readable=0>

Is this possible?

+2  A: 

Try:

element.set_attribute "readable", "0"

Or if you have a Hpricot::Elements:

elements.set "readable", "0"
jtbandes
got a syntax error: "undefined method `set' for #<Hpricot::Elem:0xb72ee64c>"
dpigera
@dpigera How are you getting the element? If you have a Hpricot::Elem you have to use set_attribute (like if you're using `doc.root`), but if you have a Hpricot::Elements, use set.
jtbandes
@jtbandes I'm getting the parent of a tag using: [email protected]("p").last.parent
dpigera
element.set_attribute did the trick!! thanks for the help jtbandes. I really appreciate it :-)
dpigera