views:

145

answers:

2

Hi all, how can I select the following element using prototype div#page_container h1 and after selecting it change its padding-top ? Thank you

+1  A: 

Make sure you include these .. and it should work , from this source

Cheers

c0mrade
+3  A: 

Prototype's $$() method allows you to select using CSS selectors, eg

var elmArr = $$("div#page_container h1");

and its setStyle method allows you to change style, eg

elmArr[0].setStyle({paddingTop: "4px"});

or

$$("div#page_container h1")[0].setStyle({paddingTop: "4px"});

Edit: I'm pretty sure the returned array is also extended by prototype, so if you had multiple h1 elements in page_container, you could do:

$$("div#page_container h1").each(function(elm){
    elm.setStyle({paddingTop: "4px"});
});

or

$$("div#page_container h1").invoke("setStyle",{paddingTop: "4px"});

to set the paddingTop of all of them

Graza
And personally, I'd use one of my last two suggestions. I use jQuery more than Prototype, and these are more in line with the philosophy of jQuery, so you're getting a little more "learning" benefit from using them
Graza
invoke takes a string as first argument : list.invoke("setStyle", myStyle);
Fabien Ménager
@Fabien - thanks, corrected
Graza