Using tritium to move html elements

552 Views Asked by At

I'm using the Moovweb SDK to make a mobile version of a website and I need to move an image from one div to another. How can I do this using tritium?

<body>
  <div class="kittens">
    <img src="images/husky.jpg" id="husky">
  </div>

  ...

  <div class="puppies">
    [need to move img here]
  </div>
</body>  
2

There are 2 best solutions below

0
On BEST ANSWER

There are a few ways to do this.

The way I find most appealing is to use some XPath axes and the move_here command:

$("./body/div[contains(@class,'puppies')]") {
  move_here("ancestor::body/div[contains(@class,'kittens')]/img[@id='huskey']")
}

This way gets the same effect using fewer characters (but is slower):

$("//div[contains(@class,'puppies')]") {
  move_here("//img[@id='huskey']")
}

The reason I like doing it the first way is that, generally, the img you're looking for will not be so close to the body. Instead of doing the ancestor::body you could do //img[@id=''huskey] but this will likely be much slower. The other benefit to using ancestor is that it shows the connection between the two elements.

0
On

You can either use move_to or move_here.

With move_to, it would look like this:

$("./div[contains(@class, 'kittens')]") {
  move_to("../div[contains(@class, 'puppies')]")
}

Test it live: http://play.tritium.io/311b7aad88ac174899dc4c2f7ebc7407db1ea08d