I’m after some help with jquery. Can some one tell me the best way to optimise the use of the .draggable and .sizable used on many different, but similarly named, elements
for example
$( "#elem1" ).draggable({
many lines of code
});
$( "#elem2" ).draggable({
many lines of code
});
$( "#elem3" ).draggable({
many lines of code
});
$( "#elem4" ).draggable({
many lines of code
});
...
$( "#elem1" ).resizable({
many lines of code
});
$( "#elem2" ).resizable({
many lines of code
});
$( "#elem3" ).resizable({
many lines of code
});
$( "#elem4" ).resizable({
many lines of code
});
...
I’m sure there is a way to simply pass the number 1, 2, 3 etc to some multi-purpose code but I’m not sure how to do this since as far as I can see you have to do the elements individually. I’m sure someone will put me right on this.
Many thanks in advance.
If your code differs only by the strings referring to a specific elements Id, you could reduce your code duplication like this:
If all your resizable elements share a common class, you could clean this up even further:
If you need to access the integer suffix specifically, you can store that value as a
dataattribute on the element itself:To take our example one step further with the markup above: