Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery show() not a function - used in adding DOM elements

I am migrating from PrototypeJS to jQuery, and I'm having issues with applying functionality to new DOM elements added into a simple banner script.

Basically, on page load, new DIV elements are being placed into the DOM using append(), as I understand, this is how it's done in jQuery as opposed to Prototype's Element object.

$.each(Banner.data, function(i, e) {
  $('#banner_area').append("<div class='banner_slot' id='bannner-"+ i +"'>...[nested elements]</div>").hide();
    });
$('.banner_slot').get(0).show();

Upon checking Firebug, the elements have successfully been added to the DOM, and are immediately hidden. Then the first (0) element should show... however, Firebug give me this error:

TypeError: $(...).get(...).show is not a function
http://www.ten103.com/javascript/global_desktop.js
Line 15

I'm sure this is something simple, as I've been using Prototype for years, but need to move to jQuery because, well... more people use it so the resources are infinitely better.

Is there some fundamental difference between the two that I'm missing here?

like image 969
mwieczorek Avatar asked Jul 11 '15 15:07

mwieczorek


People also ask

How can add DOM element in jQuery?

Answer: Use the jQuery append() or prepend() method You can add or insert elements to DOM using the jQuery append() or prepend() methods. The jQuery append() method insert content to end of matched elements, whereas the prepend() method insert content to the beginning of matched elements.

What is not a function in jQuery?

jQuery not() MethodThis method lets you specify a criteria. Elements that do not match the criteria are returned from the selection, and those that match will be removed. This method is often used to remove one or more elements from a group of selected elements.

What is DOM element in jQuery?

The Document Object Model (DOM) elements are something like a DIV, HTML, BODY element on the HTML page. A jQuery Selector is used to select one or more HTML elements using jQuery. Mostly we use Selectors for accessing the DOM elements.

Which jQuery method is used to make an element appear?

jQuery | Effect show() Method. The show() Method in jQuery is used to display the hidden and selected elements.


1 Answers

$('.banner_slot').get(0).show();

change to

$('.banner_slot').eq(0).show();

.get() returns DOM element while .eq() returns jQuery object and .show() is jQuery API.

For more information about .get() and .eq() checkout the jQuery API docs

like image 129
vinayakj Avatar answered Nov 07 '22 07:11

vinayakj