View Full Version : function return

09-21-2011, 04:30 PM
hi,i don't understand about the jquery basic example
the code is below

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
return 'par_' + n;
<style type="text/css">

<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Add classes to p elements</button>

i want to understand about the how is work function(n){ in the function a
and how work return function return 'par_' + n;
thanks mate

09-21-2011, 06:10 PM
This is one of the possible ways to use the addClass method: The first parameter of the function "n" is the index of the current element out of the jQuery element list

Let's say you have 10 <p> paragraphs on your page. Then the selector $('p') will result in a list of those paragraphs. addClass(function(n) {...}) will then add a class to each paragraph containing the text par_n. So the first paragraph will have a new class="par_0"

rnd me
09-21-2011, 09:33 PM
i guess mopst methods map to .map(), which i didn't know.
so, the long-hand would be:

$('p').map(function(n, elm){
$(elm).addClass('par_' + n);

09-22-2011, 12:29 PM
thanks for explain my dearly friend,