views:

88

answers:

2

I need to get the id attribute of a class and apply a style based on that id.

So for instance, 3 list items each with the class "typo", one id is "application", another id is "application_osx", and the final id is "application_osx_terminal"

The class "typo" is handled by CSS, but I would need to assign a background image based on the ID name.

So if the id happens to be "application_osx" or "someotherid", it would automatically have this CSS applied to it

#application_osx { background: url(/path/to/image/application_osx.png) }
#someotherid { background: url(/path/to/image/someotherid.png) }

I'm trying to use MooTools 1.1 for this.

I guess it would look like this barebones

    <html>
    <head>
    <title>Blah</title>
    <script src="path/to/mootools.js"></script>
    <script>
    A SCRIPT THAT PRINTS OUT:
    #application_osx { background: url(/path/to/image/application_osx.png) }
    #someotherid { background: url(/path/to/image/someotherid.png) }
    BASED ON THE CLASS "TYPO"
    </script>
    </head>
    <body>
    <ul>
    <li id="application_osx" class="typo">Application OSX</li>
    <li id="someotherid" class="typo">Someotherid</li>
    </ul>
    </body>
    </html>
A: 

Hi,

$$('.typo').each(function(el){
  el.setStyle('background-image', 'url(/path/to/'+el.id+'.png)')
});

Should do the trick, if I understand well…

tonio
change to `var id = el.get("id"); if (id) el.setStyle(... +id+)` - although i don't think he meant that exactly
Dimitar Christoff
get('id') has been introduced in 1.2, see http://docs111.mootools.net/Native/Element.js , was getProperty('id') before…
tonio
true, i totally missed the mootools version in the question topic... by creating a style you ensure that future adding and removing of elements with these ids within the DOM will be styled automatically as opposed to having to run your code to style them every time.
Dimitar Christoff
A: 

why can't you define the rules in your css file? if you need to dynamically produce rules and append to the document head then you need something like this

for mootools 1.2.x http://www.jsfiddle.net/dimitar/G5ywF/1/

var StyleWriter = new Class({
    // css classes on the fly, based on by Aaaron Newton's version
    createStyle: function(css, id) {
        try {
            if (document.id(id) && id) return;

            var style = new Element('style', {id: id||'',type:'text/css'}).inject(document.getElement('head'));
            if (Browser.Engine.trident)
                style.styleSheet.cssText = css;
            else
                style.set('text', css);

        } catch(e) {
            console.log("failed:", e);
        }
    }
});

use:

new StyleWriter().createStyle("#rule { blah; }\n#rule2 { blah... }\n", "mystyles");

edit it was just brought to my attention you are on mootools 1.11 so

http://www.jsfiddle.net/G5ywF/4/

class version for 1.11 with slight changes:

var StyleWriter = new Class({
    // css classes on the fly, based on by Aaaron Newton's version
    createStyle: function(css, id) {
        try {
            if ($(id) && id) return;

            var style = new Element('style', {id: id||'',type:'text/css'}).inject(document.getElement('head'));


            if (window.ie)
                style.styleSheet.cssText = css;
            else
                style.setHTML(css);

        } catch(e) {
            console.log("failed:", e.message);
        }
    }
});

tested the 1.11 class in FF 3.6x, Chromium 5 and IE7

Dimitar Christoff
Yeah, was unsure about what Gabe want…Btw, your code will need some fixes to work with 1.1…
tonio
oh yeah mootools 1.11 (/me slaps forehead)
Dimitar Christoff