Part 1 - Check marked items
To manipulate the style of DOM elements jquery provides us with several ways to do it.
All involve modifying the css property that we want, leading to the direct use of a function that performs all the manipulation of the style in an abstract way with methods such as show
, hide
...
Or on the other hand by implicitly modifying the css properties that we want from the element or elements in question with the use of the method css
or attribute
$(function(){
$('#modficarImplicitamente').css('display', 'none');
$('#modficarAbstractamente').hide();
});
* {
margin: 0px;
padding: 0px;
}
#modficarImplicitamente {
height: 50px;
background: red;
width: 50%;
}
#modficarAbstractamente {
height: 50px;
background: green;
width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="modficarImplicitamente">modficarImplicitamente</div>
<div id="modficarAbstractamente">modficarAbstractamente</div>
Once we know this we only have to execute a function every time the user clicks on each check it could be something like this
function tengoQueMostrarBoton() {
var elementos = $('input.miOpcion');
var algunoMarcado = elementos.toArray().find(function(elemento) {
return $(elemento).prop('checked');
});
if(algunoMarcado) {
$('#miBoton').show();
} else {
$('#miBoton').hide();
}
}
#miBoton {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="miOpcion" type="checkbox" value="1" onclick="tengoQueMostrarBoton()" />
<input class="miOpcion" type="checkbox" value="2" onclick="tengoQueMostrarBoton()" />
<input class="miOpcion" type="checkbox" value="3" onclick="tengoQueMostrarBoton()" />
<input class="miOpcion" type="checkbox" value="4" onclick="tengoQueMostrarBoton()" />
<input class="miOpcion" type="checkbox" value="5" onclick="tengoQueMostrarBoton()" />
<button id="miBoton"> hacer algo </button>
Part 2 - Run confirmation alert
For the second question you ask the answer is whether, if it can be done, in fact the functionality of the browser that allows us to do it is called with the function confirm
. The result of this function will be true
or false
and will help us to know the user's decision.
function eliminar() {
//implmentar logica con Ajax por ejemplo
alert("Eliminando");
}
function abrirPregunta() {
var respuesta = confirm("Desea eliminar X elementos?");
if(respuesta) {
eliminar();
}
}
<button onclick="abrirPregunta()">Eliminar</button>