0

I have the following html markup

<body>
  <div class="old-wrapper">
    ...lots of stuff
  </div>
</body>

I need to replace this with a coffeescript function to give the following markup

<body>
  <div class="new-wrapper">
    <input type="text" class="date-picker">
  </div>
</body>

I need to use .old-wrapper as the target

My coffeescript function looks something like this

jQuery ->
  $(".old-wrapper").repaceWith(
    $("<div class='new-wrapper'>") +
    $("<input type='text' class='date-picker'>").datepicker +
    $("</div>")
  )

What is the correct way to concatenate these strings within the replaceWith function? (Sorry this is a very basic javascript syntax question, but my javascript knowledge is limited)

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

4 Answers4

0

You have a syntax error to start with:

$("<div class="wrapper>")
  ^           ^        ^
open        close     ???

The rest of that code is kind of invalid as well.

Try this: $(".date-wrapper").wrapInner('<div class="wrapper" />');

You should have a look at the Tutorials section on the site: http://docs.jquery.com/Tutorials

Tim S.
  • 13,597
  • 7
  • 46
  • 72
0

Maybe like this..

jQuery ->
  $('.date-wrapper').append(
    $('<input type="text" class="date-picker" id="user_dob" value="' + dob + '" name="user[dob]" />').datepicker()
  ).wrapInner('<div class="wrapper" />')
  $('.wrapper').unwrap()

Instead of replacing the element just append your datepicker-input element, wrap it like you need it then remove wrapping element withunwrap.

edit

After rereading the question i saw that i was a bit of the wrong way ;-)

Isn't the most simple solution for your problem to simply replace all date-wrapper elements as you did already and then wrap all these by your new wrapper?

dob = '2013/4/9'
$ ->
  $('.date-wrapper').replaceWith($('<input type="text" class="date-picker" id="user_dob" value="' + dob + '" name="user[dob]" />').datepicker())      
  $('.date-picker').wrapAll('<div class="wrapper" />')

produces

<div class="wrapper">
    <input id="user_dob" class="date-picker hasDatepicker" type="text" name="user[dob]" value="2013/4/9">
</div>
<div id="ui-datepicker-div" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"></div>

see fiddle

Community
  • 1
  • 1
Felix Bayer
  • 363
  • 2
  • 11
0

This should work!

jQuery ->
    $('.date-wrapper')
        .append($('<div class="wrapper" />'))
        .find('.wrapper')
        .append($('<input type="text" class="date-picker">').datepicker())
Vitalii Ponomar
  • 10,686
  • 20
  • 60
  • 88
0

Here is my attempt. Note that I am looking for the class old-wrapper as per the original question.

I am also using the technique of putting inner calls into their own functions, which I think makes for much neater code.

Here is a jsFiddle showing the code below in action

getDatePicker=->$('<input/>', {type:'text', class:'date-picker'}).datepicker()

getWrapper=->$('<div/>', {class:'new-wrapper'}).append getDatePicker()

$ -> $('.old-wrapper').replaceWith getWrapper()
biofractal
  • 18,963
  • 12
  • 70
  • 116