javascript - (this).attr() stops working after jquery.noConflict() -


i had code worked on 1 project when decided port project, has issues $. decided use jquery.noconflict() method resolve it. resolved alright .attr() method returns undefined.

initial code

    $(".sharebtn").click(function(event) {     event.preventdefault();     content = $(this).attr("data-share-content"); content_id = $(this).attr("data-share-contentid"); medium=$(this).attr("data-share-medium");     cur_count = parseint($("#share_count_holder_"+content_id).val());     if(cur_count<=999){$("#post-share-count").html((cur_count+1));}     if(cur_count>999 && cur_count<=1000000){ disp=parsefloat(math.round((cur_count/1000)+'e1')+'e-1'); $("#post-share-count").html(disp+"k"); }     if(cur_count>1000000){ disp=parsefloat(math.round((cur_count/1000000)+'e1')+'e-1'); $("#post-share-count").html(disp+"m"); }      $("#share_count_holder").val((cur_count+1));     window.open($(this).attr("data-share-link"),"popupwindow","width=600,height=400,scrollbar=yes");     var url = bh_url+'/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';     $.post(url,{ content_type:content, content_id:content_id, medium:medium} ,function(data) { },"json");  }); 

after noconflict()

var bh = jquery.noconflict();  bh(".sharebtn").click(function(event) {     event.preventdefault();     content = bh(this).attr("data-share-content"); content_id = bh(this).attr("data-share-contentid"); medium=bh(this).attr("data-share-medium");     cur_count = parseint(bh("#share_count_holder_"+content_id).val());     if(cur_count<=999){bh("#post-share-count").html((cur_count+1));}     if(cur_count>999 && cur_count<=1000000){ disp=parsefloat(math.round((cur_count/1000)+'e1')+'e-1'); bh("#post-share-count").html(disp+"k"); }     if(cur_count>1000000){ disp=parsefloat(math.round((cur_count/1000000)+'e1')+'e-1'); bh("#post-share-count").html(disp+"m"); }      bh("#share_count_holder").val((cur_count+1));     window.open(bh(this).attr("data-share-link"),"popupwindow","width=600,height=400,scrollbar=yes");     var url = bh_url+'/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';     bh.post(url,{ content_type:content, content_id:content_id, medium:medium} ,function(data) { },"json");  }); 

the click event fires when log content console undefined. - when restore $, works fine in old project. problem.

try using iffe , passing in jquery this:

jquery.noconflict(); // releases $ other library might using  (function($) { // iife passing in jquery $, inside scope of function $ alias jquery   $(".sharebtn").click(function(event) {     event.preventdefault();     content = $(this).attr("data-share-content");     content_id = $(this).attr("data-share-contentid");     medium = $(this).attr("data-share-medium");     cur_count = parseint($("#share_count_holder_" + content_id).val());     if (cur_count <= 999) {       $("#post-share-count").html((cur_count + 1));     }     if (cur_count > 999 && cur_count <= 1000000) {       disp = parsefloat(math.round((cur_count / 1000) + 'e1') + 'e-1');       $("#post-share-count").html(disp + "k");     }     if (cur_count > 1000000) {       disp = parsefloat(math.round((cur_count / 1000000) + 'e1') + 'e-1');       $("#post-share-count").html(disp + "m");     }      $("#share_count_holder").val((cur_count + 1));     window.open(bh(this).attr("data-share-link"), "popupwindow", "width=600,height=400,scrollbar=yes");     var url = bh_url + '/admin-2/modules/blog/candor_blogger_ajax.php?action=run_share';     $.post(url, {       content_type: content,       content_id: content_id,       medium: medium     }, function(data) {}, "json");    });  }(jquery)); // pass in jquery 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -