the script below used extract url , perform image swap on mouseover. however, i'm getting uncaught type error:
cannot read property 'substr' of undefined
the code is:
<script> jquery(document).ready(function(){ jquery('.hover_box_wrapper').mouseenter(function(){ var img_src = jquery(this).parent().parent().parent().find('.hover2 img').attr('src'); var img_src_s= img_src.substring(0,img_src.lastindexof("?")); var img_href = jquery(this).parent().attr('href'); var img_src_set = jquery(this).parent().parent().parent().find('.hover2 img').attr('srcset'); var src = jquery('.center123').find('img').attr('src'); var res = src.split(" "); var src_first = res[0]; src_first = src_first.substring(0,src_first.lastindexof("?")); jquery('.center123').find('img').attr('src',img_src_s); jquery('.center123').find('img').attr('srcset',src_first); //var img_html = jquery('.center123').find('.vc_single_image-wrapper').html(); //jquery('.center123').find('.vc_single_image-wrapper').html(''); jquery('.center123').find('.vc_single_image-wrapper').html('<a href="'+ img_href + '"><img src="'+ img_src_s+'" srcset="'+ src_first +'" /></a>'); }); });
you error hidden here, in piece of code, if you'll debug line line:
var src = jquery('.center123').find('img').attr('src'); var res = src.split(" "); var src_first = res[0];
in case there's no elements found, you'll no src
attributes found, equals undefined
value stored in src_first
variable.
you need introduce additional check of result, stored within res[0]
. split(" ")
work on type {string}
, can't applied undefined
.
the simplest additional check may coded like:
// ... var src = jquery('.center123').find('img').attr('src'); if (! src) { // <== here's additional check, simplest case possible return; } var res = src.split(" "); var src_first = res[0]; // ...
Comments
Post a Comment