2

Here is my HTML

<p> checkbox list 
  <span> 
    <span>
      <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
    </span>
    <span>
      <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
    </span> 
  </span>
</p>
<p> checkbox list 02
  <span> 
    <span>
       <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
    </span>
    <span>
       <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
    </span> 
  </span>
</p>

How to wrap text 'checkbox list' and 'checkbox list02' inside a label tag in same place

i want result like this :

 <p> <label>checkbox list </label>
      <span> 
        <span>
          <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
        </span>
        <span>
          <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
        </span> 
      </span>
    </p>
    <p> <label>checkbox list 02</label>
      <span> 
        <span>
           <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
        </span>
        <span>
           <input type="checkbox" name="checkbox" value="checkbox" id="apple" />
        </span> 
      </span>
    </p>
Sanooj
  • 2,637
  • 15
  • 20

2 Answers2

7

To wrap a text subnode in a <p> container you can do something like this

$('p').contents().filter(function() {
    return this.nodeType == 3;
}).wrap('<label/>');
devnull69
  • 16,402
  • 8
  • 50
  • 61
3

Your markup is invalid.

The ID attribute needs to be unique to each element whereas you have multiple ID's of the same name.

e.g.

<p>checkbox list</p>
<label>checkbox1<input type="checkbox" value="" name="apple1" id="apple1"/></label>
<label>checkbox1<input type="checkbox" value="" name="apple2" id="apple2"/></label>

<p>checkbox list2 </p>
<label>checkbox1<input type="checkbox" value="" name="apple3" id="apple3"/></label>
<label>checkbox1<input type="checkbox" value="" name="apple4" id="apple4"/></label>

You may want to look into using an unordered/ordered list as well.

e.g.

<ul>
<li><label>checkbox1<input type="checkbox" value="" name="apple3" id="apple1"/></label></li>
<li><label>checkbox2<input type="checkbox" value="" name="apple3" id="apple2"/></label></li>
<li><label>checkbox3<input type="checkbox" value="" name="apple3" id="apple3"/></label></li>
</ul>
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94