Search code examples
jqueryscriptaculous

scriptaculous Builder equivalent in jQuery?


I like the scriptaculous Builder syntax to create DOM elements. Is there something similar in jQuery?

I'd prefer not to use something like

$("<div><p>Hello</p></div>").appendTo("body")

Solution

  • DOMBrew is an option http://jsperf.com/jquery-dom-vs-custom-dom/4

    var $b = DOMBrew;
    
    $("#thetable").append($b('tr#row-of-one').append(
      $b('td.red').append(
        $b('ul').append(
          $b('li.lists', 'one'),
          $b('li', 'two')
        )
      ),
      $b('td.blue').append(
        $b('p', 'This is a bunch of text that is right here.')
      ),
    
      $b('td.orange').append(
        $b('table#smallertable').append(
          $b('thead').append(
            $b('tr').append(
              $b('th', { colspan: 2, text: 'this is small' })
            )
          ),
          $b('tbody').append(
            $b('tr').append(
              $b('td', 'Go win.'),
              $b('td', 'Go fight.')
            )
          )
        )    
      ),
    
      $b('td.purple').append($b('#divided', 'Content'))
    ).dom());