1

im trying to move the background position of an element when a certain div is clicked only I cant seem to get it working.

My jquery seems fine from the documentation?

$(this).children('ul li').css('background-position-x','100px');

Heres a jsfiddle demo... http://jsfiddle.net/shqcu/

Liam
  • 9,537
  • 36
  • 106
  • 204
  • see also: http://stackoverflow.com/questions/648004/what-is-fastest-children-or-find-in-jquery – MikeM Jan 08 '13 at 16:58

3 Answers3

7

Use find() instead of children():

$(this).find("ul li").css("background-position-x", "100px");

DEMO: http://jsfiddle.net/shqcu/1/

VisioN
  • 138,460
  • 30
  • 271
  • 271
  • 1
    children is immediate "offspring" of the parent so your `
    ` might only have `
      ` as "children" but has lots of little `
    • ` grandchildren or descendents...`find()` gets all descendants
    – MikeM Jan 08 '13 at 16:56
  • @Liam This is because a selector in `children` is used to *filter* children elements, but not to find elements among the descendants. – VisioN Jan 08 '13 at 16:57
0

You can't alter an element's background-position with jQuery alone. You need this plugin http://keith-wood.name/backgroundPos.html

Mark Dee
  • 579
  • 3
  • 15
0
$(this).css('background-image','url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQBFaoYPOnLv_EI4sGOisRrHUGhlHjJp7iY2A4RT1PKXAAPxx3f)');
Fatima Zohra
  • 2,769
  • 2
  • 16
  • 16