Here's a short piece of code:
var $el = $("#something").find(".test"); if (!$el.length) { $("#something").append('<div class="test">somecontent</div>'); } else { $el.replaceWith('<div class="test">somenewcontent</div>'); } I couldn't find a method appendOrReplaceWith or anything similar.
Any ideas how can I make it shorter?
I believe that:
$("#something").appendOrReplace('<div class="test">sometext</div>'); would be much easier to read, but no such method is available yet.