I made a class that I got work but I think it is inefficient because I am dipping into the DOM 3 times when it could be 1 time. I don't think you need to see the rest of the code for this so I am only posting the inefficient part to keep things neat:
function showSelectedAttr(){
var productID = <?php echo $product_info['products_id'];?>;
var sizeID = 0;
var tallID = 0;
var colID = 0;
$(".sizeopt").each(function() {
if ($(this).is(':checked')) sizeID = $(this).val();
});
$(".tallopt").each(function() {
if ($(this).is(':checked')) tallID = $(this).val();
});
$(".mine_color").each(function() {
if ($(this).is(':checked')) colID = $(this).val();
});
$.ajax({
type: "POST",
url: 'get_product_attribute.php',
data: "product_id="+ productID +"&size_id="+ sizeID,
success: function( response ) {
$("#attr_container").html(response);
}
});
$.ajax({
type: "POST",
url: 'get_product_attribute.php',
data: "product_id="+ productID +"&size_id="+ tallID,
success: function( response ) {
$("#attr_container_tall").html(response);
}
});
$.ajax({
type: "POST",
url: 'get_product_attribute.php',
data: "product_id="+ productID +"&size_id="+ colID,
success: function( response ) {
$("#attr_container_color").html(response);
}
});
}
As you can see the ajax api is called 3 seperate times. Is there a better way to do this?
function showSelectedAttr() {
var productID = <?php echo $product_info['products_id'] ?>;
var sizeID = 0;
var tallID = 0;
var colID = 0;
$(".sizeopt").each(function() {
if ($(this).is(':checked')) sizeID = $(this).val();
});
$(".tallopt").each(function() {
if ($(this).is(':checked')) tallID = $(this).val();
});
$(".mine_color").each(function() {
if ($(this).is(':checked')) colID = $(this).val();
});
$.ajax({
dataType:"json",
type: "POST",
url: 'get_product_attribute.php',
data: {
productId : productID,
sizeId : sizeID,
tailId : tailID,
colId : colID
},
success: function( response ) {
$("#attr_container").html(response.Text);
$("#attr_container_tall").html(response.Tall);
$("#attr_container_color").html(response.Color);
}
});
}
Response is json format is {Text: "value", Tall: "value", Color: "value" }