简体   繁体   中英

Javascript error only on Safari

I've a javascript that works well in all browser but Safari.

The code is pretty easy and basically when I change the value of a text field (numeric value) the content of a <b> is changed

Here the code

$(document).on('change', '.priceAmount', function(e) {
 e.preventDefault();
 $((e.target).closest('div.price_info')).find('> .supplierPrice').html(
 ($(this).val() + 10);
});

the corrisponding HTML is

<div class="price_info">        
   <input type="number" class="priceAmount">    
   <b class="supplierPrice">20</b>
</div>

The 20 inside the <b> is inserted by the js when I enter 10 in the input box.

Safari gives me this error

TypeError: undefined is not a function (evaluating '(e.target).closest('div.price_info')')

It seems like Safari doesn't receive the element when the event is triggered

Use $(this) instead of e.target. Suggested to use parseInt

$(document).on('change', '.priceAmount', function(e) {
 e.preventDefault();
 $(this).closest('div.price_info').find('.supplierPrice').html(parseInt($(this).val()) + 10);
});

Fiddle

You missed the $ of the e.target :

$($(e.target).closest('div.price_info'))
//^
    .find('> .supplierPrice').html(parseInt($(this).val(), 10) + 10);

Alternatively, you can also use $(this) :

$(this).closest('div.price_info')
    .children('.supplierPrice')
    .html(parseInt($(this).val(), 10) + 10);

Syntax Error in your code:

Extra ( after html method:

$((e.target).closest('div.price_info')).find('> .supplierPrice').html(
 ($(this).val() + 10);
//^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM