Tuesday, February 12, 2013

Got multitude of jQuery approaches to offer? Thanks but no thanks, I got AngularJS

Just when I thought I already unearthed all the possible techniques for obtaining the value from a dropdown selection:

$('#CountryCode').change(function () {


 // Why use this?
 // alert($('option:selected', '#CountryCode').val());

 // Or this?
 // alert($('option:selected', $(this)).val());


 // When this will suffice:
 alert($(this).val());
});



Apparently, I lack imagination, I saw this code from a dropdown change event:

$('#CountryCode > :selected').attr('value');


Something is fundamentally wrong on that last code but I can't quite put my finger on it. Oh darn, now I remember.. that attr('value') is so leaking the abstraction!


Getting the value of something is a staple in our everyday job, hence we should give it a first-class treatment, give it a proper abstraction, i.e. we should be able to use this: $(this).val(), not a hodge-podge of stringly-typed voodoo magic that $('#Country > :selected').attr('value') is


I'm not gonna remember all the possible jQuery combinations the mankind has ever known just to obtain that elusive value from a dropdown control, what other approaches should I remember?


Ok, thanks, but no thanks, I got AngularJS, I just have to remember this: $scope.CountryCode



The question I posed above is rhetorical, but you can probably show other jQuery approaches for getting the value from a dropdown change event that I missed, be it a shorter code or long code, seriously.


Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. -- Edsger Dijkstra

No comments:

Post a Comment