Openset Openset - 1 month ago 8
HTML Question

Move input inside label with jQuery

How can I move the input inside the

<label>
tag, so that it would be
<label><input></label>
using jQuery. I need to target all the answers within the select-option set.



<div class=”select-option”>
<div>
<input id="[0]_Actual_Answer_1" name="[0]_Actual_Answer_1" type="radio" value="IZWE">
<label for="[0]_Actual_Answer_1">HELLO WORLD</label>
</div>
<div>
<input id="[0]_Actual_Answer_2" name="[0]_Actual_Answer_2" type="radio" value="IZWE">
<label for="[0]_Actual_Answer_2">HELLO WORLD 2</label>
</div>
<div>
<input id="[0]_Actual_Answer_3" name="[0]_Actual_Answer_3" type="radio" value="IZWE">
<label for="[0]_Actual_Answer_3">HELLO WORLD 3</label>
</div>
<div>
<input id="[0]_Actual_Answer_4" name="[0]_Actual_Answer_4" type="radio" value="IZWE">
<label for="[0]_Actual_Answer_4">HELLO WORLD 4</label>
</div>
</div>




Answer

Your quotes <div class=”select-option”> are incorrect! Use "
Than for the jQuery part:

$("label[for*='[0]_Actual_Answer']").prepend(function(){
  return $(this).prev("input");
});
label{background:red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select-option">
  <div>
    <input id="[0]_Actual_Answer_1" name="[0]_Actual_Answer_1" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_1">HELLO WORLD</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_2" name="[0]_Actual_Answer_2" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_2">HELLO WORLD 2</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_3" name="[0]_Actual_Answer_3" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_3">HELLO WORLD 3</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_4" name="[0]_Actual_Answer_4" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_4">HELLO WORLD 4</label>
  </div>
</div>