Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Uncaught TypeError: Cannot read property 'nodeType' of undefined

I use jQUery UI Position plugin: http://jqueryui.com/position/ to position my icons on a web page. The selectors are grabbed from the database and outputted to JS using PHP in a $myselector variable. This is my current code:

var element_selector='<?php echo $myselector;?>';

$('#inline_docxdiv .Featured.Slider').position({
my: "center",
at: "right top",
of: $(element_selector)

});

//append icons,applicable to all

$(divname<?php echo $uniqueid;?>).append('<div id="inline_docxdiv" class="<?php echo $uniqueid;?>"><div id="helpericons_display"><a class="<?php echo $title_toolsetdisplayed;?>" id="questionmarkicon_inlinedoc" title="Display Explanation"><img src="<?php echo $helper_iconpng;?>"></a><a target="_blank" href="<?php echo admin_url().'post.php?post='.$id_toolsetdisplayed.'&action=edit';?>" class="<?php echo $title_toolsetdisplayed;?>" id="sourceicon_inlinedoc" title="View source"><img src="<?php echo $helpersource_iconpng;?>"></a></div></div>');

However the icons are not appended correctly and it returns an error in the console:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

The strange thing is that if I hard-code the selector in the JS code (not outputted by PHP), everything works OK and no error returned in the console. This is the code where I hard-coded the element selector:

var element_selector='.idoc-featured-slider';

Is there a way to use PHP to output the selector and not encountering the error? Thanks for any help.

like image 230
Emerson Maningo Avatar asked Apr 03 '13 01:04

Emerson Maningo


2 Answers

I ran into a similar issue. I was getting the following error:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

With these dialog position configuration values:

position: {my: "center", at: "left top", of: "window"}

Per the jQuery UI documentation, the value of the "of" property is an object rather than a string. So, when I changed the position values to the following:

position: {my: "center", at: "left top", of: window}

the error disappeared.

like image 55
Leif Nelson Avatar answered Nov 17 '22 01:11

Leif Nelson


The problem is due to the of:$() not working if the object/selector is not valid.

like image 32
user2120978 Avatar answered Nov 17 '22 01:11

user2120978