-1

can any body tell me why my code error on firefox?

HTML:

Size : <select class="size" id="size<?php echo $dat_women->id; ?>" name="size" onchange="change_size(<?php echo $dat_women->id; ?>)">

JS:

function change_size(id_product){
    var size  = document.getElementById(event.target.id).value;
    $.ajax({
        dataType:"json",
        url: site_url + "main/add_cart/" + id_product + "/" + size,
        success:function(data){ 
            window.location.assign("cart");
        },
        error: function(data){
            alert('Anda belum memilih ukuran');
        }
    });
};

Error:

event is not defined

abc123
  • 17,855
  • 7
  • 52
  • 82

2 Answers2

1

Try sending the element as a parameter to the function call:

In your HTML:

onchange="change_size(this,<?php echo $dat_women->id; ?>)"

and in your function defnition:

function change_size(element, id_product)

then you can get the size like this:

var size = element.value;
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

Plunker Example

HTML:

<select class="size" id="size2" name="size" onchange="change_size(this, 2)">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
</select>

JS:

function change_size(id_product){
    var size  = $(this).value;
    $.ajax({
        dataType:"json",
        url: site_url + "main/add_cart/" + id_product + "/" + size,
        success:function(data){ 
            window.location.assign("cart");
        },
        error: function(data){
            alert('Anda belum memilih ukuran');
        }
    });
};
abc123
  • 17,855
  • 7
  • 52
  • 82