htmlrubytagshpricot

hpricot add attribute to a HTML tag?


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?


Solution

  • Try:

    element.set_attribute "readable", "0"
    

    Or if you have a Hpricot::Elements:

    elements.set "readable", "0"