I think the button
with jQuery
works for me but I can't make the labels span
recognize them with html
.
$(document).on('click', '#Crear', function() {
var boton = $('<button/>', {
'type' : 'button',
'class' : 'editar edit-modal btn btn-warning',
'id' : 'ButtonEditar',
'text' : '<span class="fa fa-edit"></span><span class="hidden-xs"> Editar</span>'
});
$( '#contenedor').append(boton.prop('outerHTML'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<button type="button" id="Crear" class="btn btn-primary">
<span class="fa fa-plus"></span>
<span class="hidden-xs"> Agregar</span>
</button>
<div id="contenedor">
</div>
You just have a little bug instead of using
text
usehtml
.text
it is only for text andhtml
is rendered as an element of theDOM
Taking into account the proposal of @User2930137 only a small modification but it is exactly the same, maybe a little more complex
The modification consists of only doing a solo
.append()
in the following way:What is really being done here is to create two elements and when concatenating with
.append()
they are separated by,
.This avoids making 2
.append()
of two elements$('#contenedor').append(boton.append(span).append(span2));
like @User2930137 does .Final score
Instead of putting the
span
in the attributetexto
, after creating the button, and taking itsID
you put it in theinnerHTML
The way you were trying to do this with jQuery would be to create three objects; one the button and the other the span and make it just like you intended.
I borrowed the code from @JuankGlezz and modified what is necessary for this example.
You can create the button from a string literal