I have a button that I modify with jQuery, to change it’s id and/or class. And when it has a spesific class and user clicks the button, it should execute the following script:
$(document).ready(function() {
$(function() {
$('.error').hide();
$("#tilisiirtobtn").click(function() {
alert("buttoni toimii");
var Nimi = $(".Nimi").val();
var Osoite = $(".Osoite").val();
var Postinumero = $(".Postinumero").val();
var Postitoimipaikka = $(".Postitoimipaikka").val();
var Puhelin = $(".Puhelin").val();
var Sahkoposti = $(".Sahkoposti").val();
var Filtteri = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
$('.error').hide();
if(Nimi == ""){
$(".Nimi").focus();
$("#Nimi-error").show();
return false;
}
if(Osoite == ""){
$(".Osoite").focus();
$("#Osoite-error").show();
return false;
}
if(Postinumero == ""){
$(".Postinumero").focus();
$("#Postinumero-error").show();
return false;
}
if(Postitoimipaikka == ""){
$(".Postitoimipaikka").focus();
$("#Postitoimipaikka-error").show();
return false;
}
if(Puhelin == ""){
$(".Puhelin").focus();
$("#Puhelin-error").show();
return false;
}
if(Sahkoposti == ""){
$(".Sahkoposti").focus();
$("#Sahkoposti-error").show();
return false;
}
$('.supernappula').attr('disabled', 'disabled');
var data = $('#yhteystiedot').serializeArray();
data.push( { name: 'cartContent', value: $('#emailedcart').html()});
//alert (data);return false;
$.ajax({
type: "POST",
data: data,
url: "order/order.php",
dataType: "html",
error: function(){ if(confirm("Mitään ei näytä tapahtuvan. Päivitä sivu?") == true){ window.location.reload();}
},
success: function() {
alert("Onnistui");
}
});
return false;
});
});
});
However, it won’t even execute the alert that is the first thing to do.
Here’s the code that changes the id and class of the button:
$(function() {
$("#paypal").click(function() {
$(".yhteystiedot").slideUp(600);
$(".toimitustapa").slideDown(600);
$('form :input').val("");
$('.supernappula').html("Maksa PayPalissa");
$('.supernappula').addClass("simpleCart_checkout");
$('.supernappula').attr("id", "checkoutbtn");
});
$("#tilisiirto").click(function() {
$(".yhteystiedot").slideDown(600);
$(".toimitustapa").slideDown(600);
$('.supernappula').html("Tee tilisiirto");
$('.supernappula').removeClass("simpleCart_checkout");
$('.supernappula').attr("id", "tilisiirtobtn");
});
$("#postiennakko").click(function() {
$(".yhteystiedot").slideDown(600);
$(".toimitustapa").slideDown(600);
$('.supernappula').html("Maksa postiennakolla");
$('.supernappula').removeClass("simpleCart_checkout");
$('.supernappula').attr("id", "postiennakkobtn");
});
$("#matkahuolto").click(function(){
$(".maksu").slideDown(600);
});
$("#posti").click(function(){
$(".maksu").slideDown(600);
});
});
And the button itself without modifications:
<button type="button" id="checkoutbtn" class="simpleCart_checkout supernappula"></button>
Here’s a fiddle of the whole page.
If anyone understands?
The most likely resolution to this is that you need to use jQuery’s
livefunction rather thanclick.With the click handler, it registers everything on the page as it is and does not register the event for elements in the future which will have the given selector.
livesolves this problem by adding the event handler to all elements now, and in the future.http://api.jquery.com/live/
Changing this line:
$("#tilisiirtobtn").click(function() {to
$("#tilisiirtobtn").live('click', function() {Should solve your problem.
Here’s a working jsFiddle