javascript - How to add css to a specific Array element on Jquery -
i'm trying develope code generates random rgb color each element name "title" on website.
first, choose all elements specific class "title":
var element = $('.title')
then, save on var called n, total number of titles (used on for)
var n = element.length
now. i'm trying traverse title elements , adding css random rgb color. note: use element[ ], access each of array element. correct?
for (var = 0; < n; i++) { element[ ].css({ "background": randomcolor(); }) }
however doesn't work... how can solve that? idea.
regards.
element[i]
refers dom element not have method css() provided jquery, need jquery wrapper object element @ index i
can obtained .eq()
for (var = 0; < n; i++) { element.eq(i).css({ "background": randomcolor(); }) }
you can pass function .css() like
element.css('background', function () { return randomcolor() })
Comments
Post a Comment