0

One thing i can't understand about jQuery, maybe someone can explain me this.

for example: HTML:

<div>
<img class="class1" src="file.jpg" />
</div>
<div>
<img class="class2" src="file.jpg" />
</div>

jQuery:

(function ($) {

    $(document).ready(function () {
        $('.class1, .class2').myfunction();
    });

    function myfunction(param1, param2) {
        var img = $('img', param1);
    }

})(jQuery);

So the question is what exactly in param1 and in param2, if function has been called like that(without passing parameters to the function)?

4
  • 1
    You can always console.log(arguments) in a function body to see the parameters being passed. Commented Sep 24, 2013 at 18:01
  • you executed a function that doesn't exist. myFunction is not a property of $.fn Commented Sep 24, 2013 at 18:03
  • Even if you did define myfunction properly, there would be no params because you didn't pass any! Commented Sep 24, 2013 at 18:04
  • Thank you Kevin B, now i understand where is my mistake. Commented Sep 24, 2013 at 18:09

1 Answer 1

1

The function call should be as follows:

View:

<div>
    <img id="img1" src="file.jpg" />
</div>
<div>
    <img id="img2" src="file.jpg" />
</div>

JQuery:

(function ($) {
    $(document).ready(function () {
        var path1 = $('#img1').attr('src');
        var path2 = $('#img2').attr('src');
        myfunction(path1, path2);
    });

    function myfunction(param1, param2) {
        // your code
    }
})(jQuery);
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, thank you. Or $.fn.myfunction = function(){myfunction($('div'),'param2')} then my example will be right too. But thanks.

Your Answer

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