patternjavascriptMinor
New Game - Am I going down the right path for DOM manipulation?
Viewed 0 times
paththegoingnewgamedownfordommanipulationright
Problem
I find myself using a lot of
Is using
JS Fiddle Example: http://jsfiddle.net/hgwNU/6/
Example Script:
(
Called with:
JS Fiddle Example: http://jsfiddle.net/hgwNU/6/
I suppose I'm just curious if I'm off to a good start for how to manipulate both the
.find functions within what I'm developing. The reason being that I need to run this same function on a TON of different characters eventually, and the easiest way I could find to do this was make appropriate sections classes and wrap each character in their respective ID.Is using
.find a ton (to only target the classes within the #characterId) considered bad practice?JS Fiddle Example: http://jsfiddle.net/hgwNU/6/
Example Script:
(
x is the internal javascript object, where y is the DOM ID for what to target)function updateDom(x,y){
if(x.pc = 4){
y.find('.addSpeed').attr('disabled', 'disabled');
}
if(x.speed 1){
y.find('.remSpeed').removeAttr('disabled');
}
if(x.speed 0){
y.find('.addSpeed').removeAttr('disabled');
}
y.find(":checkbox").each(function() {
if($(this).data('pc') > x.pc && $(this).prop('checked')==false){
$(this).attr('disabled', 'disabled');
} else if ($(this).data('pc') <= x.pc) {
$(this).removeAttr('disabled');
}
});
for (var key in x) {
y.find('#' + key).text(x[key]);
};
}Called with:
updatePlayer(bulk,$('#bulkUi'));JS Fiddle Example: http://jsfiddle.net/hgwNU/6/
I suppose I'm just curious if I'm off to a good start for how to manipulate both the
GUI and the internal object effectively, easily, quickly and repeatedly. Looking for best practices if anybody has any feedback thus far.Solution
Personally instead of using
Which will find all instances of
.find I prefer passing context to the jQuery selector like this;$('.remSpeed', y);Which will find all instances of
.remSpeed that are within the element y.Code Snippets
$('.remSpeed', y);Context
StackExchange Code Review Q#46352, answer score: 6
Revisions (0)
No revisions yet.