0

I'm trying to format and change the structure of a standard HTML form. At the moment the output looks like this

    <form accept-charset="UTF-8"  autocomplete="off" method="POST">
            <ol class="questions">
                <div>
                     <label>First Name *</label>
                     <input type="text" />
                </div>
                <div>
                     <label>Email *</label>
                     <input type="text" />
                </div>
                <div class="submit">
                      <input type="submit" value="Submit" />
                </div>
            </ol>
      </div>
    </form>

I need to change the divs to list items and then add a span around the labels if possible.

I've got this so far but it's not altering unfortunately.

  window.onload = function() {        
    var widgetHTML = $('ol.questions').html();
        widgetHTML = widgetHTML
            .replace(/<div>/g, '<li>')
            .replace(/<\/div>/g, '</li>');
    $('ol.questions').html(widgetHTML);
  };

Any ideas to add the span and replace the DIVs to LIs that would be great

j08691
  • 204,283
  • 31
  • 260
  • 272
DIM3NSION
  • 1,681
  • 5
  • 20
  • 38

3 Answers3

1

Use .replaceWith() and .wrap():

$("form div").replaceWith(function(){
    $(this).find('label').wrap('<span/>');
    return $('<li>' + this.innerHTML + '</li>')
});

jsFiddle example

This will give you:

<form accept-charset="UTF-8" autocomplete="off" method="POST">
    <ol class="questions">
        <li>
            <span><label>First Name *</label></span>
            <input type="text">
        </li>
        <li>
            <span><label>Email *</label></span>
            <input type="text">
        </li>
        <li>
            <input type="submit" value="Submit">
        </li>
    </ol>
</form>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • If I wanted to progress this further, and wrap the contents of the form inside a div, would I create a separate function? – DIM3NSION Nov 19 '14 at 16:13
  • Thanks for the fast response, - I think I confused things a little I mean to wrap a div called form-contents inside the form tag not the form within a div. :) – DIM3NSION Nov 19 '14 at 16:32
  • great help. With your existing code, this replaces all the classes that were on the div's. Is there anyway to preserve the class names. So for instance
    turns into
– DIM3NSION Nov 19 '14 at 17:06
  • You could change the return line to `return $('
  • ' + this.innerHTML + '
  • ')` but that would include undefined classes for divs that didn't have a class, so you might want to expand the logic to check for empty classes first. – j08691 Nov 19 '14 at 17:16
  • I see where your going with that. To be honest, I only really want to ensure the last LI is actually a div element. and the OL list closes. Would this be on the right lines? $(''}).insertBefore('.questions li:last-child'); – DIM3NSION Nov 19 '14 at 17:30
  • I'm pretty sure this can be done within the first function where the DIVS change to LI's. Is there way to exclude the last div? – DIM3NSION Nov 19 '14 at 17:31