javascript - How to pass jquery selections into a function? -


i tried searching, couldn't quite find looking for. super simple someone.

this code i'm starting i'd turn function. there way don't have repeat.

var fullwidth = $('.full-img').width(); var paddbottom = fullwidth * .75; $('.full-img').css('padding-bottom', paddbottom);  var thumbwidth = $('.img-box').width(); var thumbbottom = thumbwidth * .75; $('.img-box').css('padding-bottom', thumbbottom); 

here's tried

function aspectratiofix(main, thumb) {     var fullwidth = main.width();     var paddbottom = fullwidth * .75;     main.css('padding-bottom', paddbottom);      var thumbwidth = thumb.width();     var thumbbottom = thumbwidth * .75;     thumb.css('padding-bottom', thumbbottom); }      aspectratiofix('.full-img', '.img-box'); 

i error "uncaught typeerror: main.width not function"

is there way maybe loop through function take care of both parts of script instead of duplicating have? thanks!

the function aspectratiofix expecting jquery objects you'll have pass in jquery objects instead of selectors (strings) this:

aspectratiofix($('.full-img'), $('.img-box')); 

if have many images , thumbs , want apply code of them use .each this:

var $mains = $('.full-img'),     $thumbs = $('.img-box');  $mains.each(function(index) {     // this:  main image     // index: index can access according thumb $thumbs      aspectratiofix($(this), $($thumbs[index])); // apply aspectratiofix on main image, , on it's thumb $thumbs object (both , $thumbs[index] node elements have wrap them in jquery object using $()) }); 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -