Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - adding parent

I am new to jQuery and i have a problem.

I have script that for resize each image in some div. And i want to insert that image into non existing div (create div-parent), how?

EDIT:

I've got this code:

$('#content img').each(function() {

            if( $(this).width() > maxWidth || $(this).height() > maxHeight )
            {   
                var ratio = 0;
                if( $(this).width() > $(this).height() )
                {
                    ratio = maxWidth / $(this).width();
                    $(this).css('width', maxWidth + 'px');
                    $(this).css('height', ( $(this).height() * ratio) + 'px' );
                }
                else
                {
                    ratio = maxHeight / $(this).height();
                    $(this).css('width', ($(this).width() * ratio) + 'px');
                    $(this).css('height', maxHeight + 'px');
                }

                $(this).addClass('scaled-img');
                $(this).wrap('<div class="test"></div>'); // trying to add

            }                               

            });
});

Result:

<img src="path.."/>

Result that i want:

<div><img src="path"/></div>
like image 314
Robik Avatar asked Mar 17 '11 13:03

Robik


People also ask

What is parent () parent () jQuery?

The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

How do I find a specific parent in jQuery?

The parents() is an inbuilt method in jQuery which is used to find all the parent elements related to the selected element. This parents() method in jQuery traverse all the levels up the selected element and return that all elements.

What is the use of parent () and child () method in jQuery?

The ("parent > child") selector selects all elements that are a direct child of the specified element.

What does $( Div parent will select?

The :parent Selector page on jQuery says: Select all elements that have at least one child node (either an element or text). So $('div') would select all divs and $('div:parent') would select only those with children.


2 Answers

Use .wrap().

HTML

<div id="some-div">
    <img ... />
    <img ... />
    <img ... />
</div>

JavaScript

$(function ()
{
    $('#some-div > img').wrap('<div class="new-parent"></div>');
});

Result

<div id="some-div">
    <div class="new-parent"><img ... /></div>
    <div class="new-parent"><img ... /></div>
    <div class="new-parent"><img ... /></div>
</div>

Demo (now with extra kittens!)

like image 138
Matt Ball Avatar answered Oct 30 '22 11:10

Matt Ball


Look at the ".wrap()" jQuery method. It lets you wrap en element in some other container element:

$('#myImage').wrap('<div></div>');

If you need to give the container some properties:

$('#myImage').wrap($('<div></div>', {
  id: "newWrapper",
  class: 'wrapper banana',
  css: { 'borderColor': 'green', 'borderWidth': '2px' }
}));
like image 23
Pointy Avatar answered Oct 30 '22 12:10

Pointy