0

I have some element in my html code like following:

<DIV id="slideshow-container">
    <DIV id="slideshow">
        <ul>
            <li>...</li>
            <li>...</li>
            <li>...</li>
        </ul>
    </DIV>
</DIV>  

Now i will get li elements and set width of that with jquery. But my code not work.
The following is my code:

var slides = $('#slideshow li');
var w=$('#slideshow').width();

for(var j=0;j<slides.length;j++){
    slides.get(j).css('width',w);
}

Do you know where is my mistake?
Thanks.

0

7 Answers 7

3

Actually, this...

slides.css('width', w);

... should be sufficient. As .css is used as a setter, quoting the doc:

...set one or more CSS properties for every matched element.

Demo.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd change:

for(var j=0;j<slides.length;j++){
    slides.get(j).css('width',w);
}

to:

$('#slideshow li').each(function(){
    $(this).width(w);
});

Comments

0

Change

 slides.eq(j).css('width',w);

To

 slides.eq(j).css('width',w+'px');

x.width() return 80

While in css it should be with unit :

x.css('width','80px')

Comments

0

slides.get(j) returns the DOM object for each <li>, rather than the jQuery object.

This should work:

$( slides.get(j) ).css('width',w);

Or, instead of the for loop:

slides.each(function () {
    $(this).css('width', w);
});

Comments

0

You're creating an array of those elements, but you actually have to change them on the view, use an each and modify this

var w=$('#slideshow').width();
$("#slideshow ul li").each(function() {
    $(this).css("width", w);
});

Comments

0

Try replacing

for(var j=0;j<slides.length;j++){
    slides.get(j).css('width',w);
}

with

slides.each(function () {
    $(this).css('width', w + 'px');
});

Comments

0

jQuery Solution (slower, easier to implement)

If jQuery is required for other things in your site, and speed is not really a concern to you, here's the jQuery way to achieve the same result:

$("#slideshow li").width(w);


Non-jQuery solution (faster)

jQuery is significantly slower than simple javascript dom manipulation.

Create a simple library for javascript you need - reuse is good.

jQuery is a very large library for doing something so straightforward.
Note: Example uses namespacing. Encapsulation is better, but namespacing is a good start.

var myNS = {
    elById: document.getElementById,
    elsByParentId: function (id, optionalTagName) {
        var tag = optionalTagName || "*";
        return myNS.elByID(id).getElementsByTagName(tag);
    }
    setWidthForEls: function (els, w) {
        for (var i=0, c=els.length; i < c; i++){
        els[i].style.width = w;
    },
    getWidthOfEl: function (el){
        return el.style.width;
    }
    setWidthForElsFromEl: function (els, el) {
        myNS.setWidthForEls(els, myNS.getWidthOfEl(el));
    }
}  

Then, in the code section you are referring to, you just do:

myNS.setWidthForElsFromEl(
    myNS.elsByParentID("slideshow","li"),
    myN.elByID("content")
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.