1

I keep running up against this issue, which reflects my basic lack of understanding.

I want to collect all instances of a class, then be able to have a function which understands which element instance called it.

I created a Fiddle, http://jsfiddle.net/AKa4s/, which doesn't work, just to try to explain my question.

It's like this, I collect all the div elements & assign a function to it: $(".square").each(writeColorpanel);

Then in that function I need to call another function. The next function seems to have absolutely no idea who called. I need caller ID (joke).

The problem is that I end up running the same calculations for different divs, and repeating the function so that I can apply css to each of the divs based on the results.

5
  • 2
    You can't have multiple elements with the same id attribute value. Commented Aug 6, 2012 at 14:06
  • 1
    Where is the .doChange() method coming from? Is it a plug-in? Commented Aug 6, 2012 at 14:08
  • My guess is that the "current element" is referenced by the this value within the onColorChange function. Commented Aug 6, 2012 at 14:10
  • It was just an example, but yes I am using values returned from Farbtastic in this case. My example may be poor... I don't know how to express the question very well. How to run calculations based on values retrieved from some divs, then apply values to all those divs. A basic structure question. I cannot find any syntax such as $(this) or $(elem) to work. Commented Aug 6, 2012 at 14:11
  • Check what this refers to inside the onColorChange function. It could be the reference you're looking for... Commented Aug 6, 2012 at 14:15

2 Answers 2

1

You're doing it way too complicated.

jQuery(document).ready(function($) {


// run the function for every div
$(".square").each(function(i) {

    $('.output').eq(i).val(multiplyColor($(this).val()));
    //Show the output to the 1st/2nd/3rd output (that's where eq comes from
    //As for the value, I call a function and pass a parameter to do the calculations

    //Now if you want to call another function you need to pass the element itself as a parameter;
    alertMyId($(this));
});

function alertMyId(elem) {
   //elem has now all the same properties as if you were in the "each" function      
    alert(elem.attr('id'));

}

//Ideally if you just want to calculate something, you should just pass along the value you want to calculate
function multiplyColor(value) {
  var color = 3;
  return  value * color;
}

}); Check this fiddle

Also you can't have different html elements with the same ID. Use classes instead.

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

3 Comments

Added code, in case of dead-link or jsFiddle being in maintenance preventing other users to see the code in the future.
The Ids were a typo, sorry. My question is, what if you have to call another function? In the first function you can access the elem, because you are attaching the function to it. But now what if each 'square' has a value, & you need to call another function to run calculation on the value, and then place those values back into the divs that you began with?
@JenniferMichelle You simply need to pass the element (or juste the ID) as a parameter. I will update my answer with an example.
0

Is it something like this :

$(function() {
    $(".square").each(function(i,e) {
        writeColorpanel(i,e);
    });

    function writeColorpanel (i, elem) {
       color=3;        
       onColorChange(elem, color);
    }

    function onColorChange(elem, color) {
       var m = elem.value
       var q = m*color;
       $('#output_'+elem.id).val(q);
    }
});

FIDDLE

4 Comments

Please explain what you changed.
He passed the element to the onColorChange function so that is your "caller id" :)
I understand. I think I have tried this before and it didn't work, but your example obviously does. Thanks, I'll try that in the long icky noob code I have. :)
This works fine, it's the basic way of passing something on to a function in it's simplest form.

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.