How to insert element inside another dynamically created

Asked

Viewed 104 times

1

When I load the page, I have some checkbox:

<input type="checkbox">

And when I get to the ready of the page, I want to put the checkbox inside a label with class 'checkboxPersonalized', being as follows:

<label class="checkboxPersonalizada">
     <input type="checkbox">
     <i></i>
</label>

2 answers

0

It was not very clear your question. But it would be something like?

	$(document).ready(function() {
	  var conteudo = $(".checkboxPersonalizada").html();
	  var check = "<input type='checkbox'>";
	  $(".checkboxPersonalizada").html(conteudo + check)
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkboxPersonalizada">
  oi
  <i></i>
</label>

0


I believe I can use the jquery wrap to achieve what you want from the string shape.

$(document).ready(function(){
  var checkboxElment = $('#checkboxWrapped');
  var wrapper        = $('<label/>').addClass('checkboxPersonalizada').text();
  var extraElem      = $('<i/>').text();

  checkboxElment.wrap( wrapper ).parent().append( extraElem );
});

link: http://codepen.io/anon/pen/pRyRJR

I left the text property if you want to add something.

  • Gave straight!!! Muuuuuuuuuuuuito thanks!!! : D.. I did not know the wrap..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.