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
views:
145answers:
2
+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
2010-01-05 11:44:50
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
2010-01-05 12:21:48
invoke takes a string as first argument : list.invoke("setStyle", myStyle);
Fabien Ménager
2010-01-05 12:38:46
@Fabien - thanks, corrected
Graza
2010-01-05 12:55:44