I have attached a onClick event handle to a tag. On click, I wanted to pick-up an attribute — let’s say, fn; and I want to call the function mentioned as the attribute value.
The following code does not work
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script language="JavaScript">
var Test = Test? Test : new Object();
$(document).ready(function(){
Test.main();
})
Test.sub = function(){
alert('called');
}
Test.main = function(){
$('.caller').click(function(e){
e.preventDefault();
var fn = $(this).attr('fn');
alert('calling: '+fn);
return fn();
});
}
</script>
</head>
<body>
<a class="caller" fn="Test.sub" href="#">test call</a>
</body>
</html>
My questions are
- Is it possible?
- What am I doing wrong?
- What could be right approach to solve this issue?
Thanks
Nishant
Yes. If you change your link to just have
sub:Then this works:
The value you retrieve is a string. You can index into an object’s properties using a string if you like using bracketed notation (
[]), so all of these are equivalent:You were nearly there. You had “Test.sub” in a string, but as it was a string, not a function, you couldn’t call it. It would be like doing this:
….which obviously isn’t quite doing what you want. 🙂
In technical terms, the above solves the problem. Part of me worries about having the actual function name in an attribute and wants to say “have a lookup table instead”, but your
Testobject basically is the lookup table, so…Side note: In this situation, you may see people using
eval. Usually that’s because they didn’t realize you could index into an object with bracketed notation.evalis best avoided.Off-topic #1: Your attribute,
fn, is invalid (invalid = won’t validate). In HTML4 and earlier, all custom attributes were invalid, although every desktop browser I’ve ever seen allows them (browsers let us get away with lots of things). As of HTML5, the demand for this feature was heard and we can have valid custom attributes, but they must start withdata-so the validator knows they’re custom. So if validation is part of your workflow (and it’s usually a good idea), you might go withdata-fn="sub"rather than simplyfn="sub". More here.Off-topic #2: If you want to save some typing, use
{}rather thannew Object(). E.g.:And if you want to save even more, use the curiously-powerful
||operator: