Fajeth88 Fajeth88 - 1 year ago 37
HTML Question

Trying to assign a functions return value to a variable

I have a script that checks whether the value of a selected element equals the span id. Everything works fine except for the variable assignment.
To be more precise: The function itself works (if i alert object.id it is displayed right) but the variable assignment doesn't. If I try to alert the variable, it says it's undefined. I'm sure it's some minor mistake and it would be very nice if someone could point it out:

var spanId = (function getId(object)
return object.id;

Answer Source

Setting the variable will not update that variable when you call it again and again. You need to restructure your code in order to update that variable.

var spanId;
function getId(object) {   
    spanId = object.id;

now spanId will have the current value.