Envoi de variables à une fonction
Bonjour à tous,
Je suis débutant avec JQUERY et j'ai un petit souci pour envoyer des variables à une fonction.
Voici le code HTML :
Code:
<a href="#" class="alertElement">Bouton suppression</a>
Voici ma fonction JQUERY :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| (function($) {
$.confirm = function(params) {
if ($('#confirmOverlay').length) {
// A confirm is already shown on the page:
return false;
}
var buttonHTML = '';
$.each(params.buttons,function(name,obj) {
// Generating the markup for the buttons:
buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'</a>';
if (!obj.action) {
obj.action = function() {};
}
});
var markup = [
'<div id="confirmOverlay">',
'<div id="confirmBox">',
'<h1>',params.title,'</h1>',
'<p>',params.message,'</p>',
'<div id="confirmButtons">',
buttonHTML,
'</div></div></div>'
].join('');
$(markup).hide().appendTo('body').fadeIn();
var buttons = $('#confirmBox .button'),
i = 0;
$.each(params.buttons,function(name,obj) {
buttons.eq(i++).click(function() {
// Calling the action attribute when a
// click occurs, and hiding the confirm.
obj.action();
$.confirm.hide();
return false;
});
});
}
$.confirm.hide = function() {
$('#confirmOverlay').fadeOut(function() {
$(this).remove();
});
}
})(jQuery); |
Que je personnalise avec une seconde fonction :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
$(document).ready(function() {
$('.alert').click(function(e) {
e.preventDefault();
var theHREF = $(this).attr("href");
var elem = $(this).closest();
$.confirm({
'title' : 'Suppression',
'message' : 'Etes-vous sûr de vouloir supprimer cet élément et son contenu ?',
'buttons' : {
'Je confirme' : {
'class' : 'blue',
'action': function() {
window.location.href = theHREF;
}
},
'Annuler' : {
'class' : 'gray',
'action': function() {} // Nothing to do in this case. You can as well omit the action property.
}
}
});
});
}); |
Est-il possible de définir le "title" et "message" dans le fichier HTML directement, afin d'avoir des messages plus personnalisés que "élément".
Jusqu'ici je duplique bêtement la seconde fonction en donnant une autre class et donc en mettant un autre texte... et je sais que c'est moche :cry:
Je sais que je devrais utiliser des variables mais comment les passer à la fonction.
Merci d'avance