jQuery before() method is breaking a generated sub menu when using jQuery 1.9.1, fine on previous version, -


i building select drop down menu mobile devices based on pre-existing ul, working fine old version of jquery (1.4.1). however, have updated jquery version 1.9.1 , code breaks menu. section breaking child menu of section viewing.

var mobilemenu = function (menuparent, prevsibling) {      var $select = $('<select>', {         class: 'mobilemenu'     }).insertafter(prevsibling);      $(menuparent).each(function () {         var $li = $(this),             $a = $li.find('> a'),             $p = $li.parents('li'),             prefix = new array($p.length + 1).join('-');          var $option = $('<option>')             .text(prefix + ' ' + $a.text())             .val($a.attr('href'))          if ($(this).hasclass("selected")) {             $option.attr('selected', true);         }          $option.appendto($select);          if ($(this).hasclass("selected")) {             mobilesubnav($select);         }      });      $(".mobilemenu").change(function () {         window.location = $(this).find("option:selected").val();     }); };  var mobilesubnav = function (navcontainername) {     $('.main_nav_sub li').each(function () {         var $li = $(this),         $a = $li.find('> a'),         $p = $li.parents('li'),         prefix = new array($p.length + 1).join('-');          var $option = $('<option>')         .text(prefix + '--' + $a.text())         .val($a.attr('href'));         $option.before(navcontainername);     }); } 

it appears 3rd last line - before() method. see on upgrade guide jquery version 1.9 states 'attempting use .after(), .before(), or .replacewith() on node without parent has no effect'. mean script breaking becuase parent element not yet added dom? shed light on happening , point me in firrection fix it?

thanks

lots of things need changed here compared 1.4.1. sure use jquery migrate, find many of them.

class: 'mobilemenu'

that's been error on browsers regardless of jquery, need quote word class since it's keyword:

'class': 'mobilemenu'

the use of .attr() setting properties has been deprecated since 1.6, should .prop() instead:

$option.attr('selected', true);

on question .before() not sure why worked in first place. seems navcontainername select element , if should using .appendto() or .prependto() add options.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -