I was wondering if it were more efficient/faster to change multiple attributes with jQuery or to just change replace all the html in one go. This is the code i am using at the moment.
// shows the table and changes the image to up showTable = function(tableId){ $('#img' + tableId).attr("src", images["up"]).attr("alt", "Hide Table").attr("title", "Hide Table"); $('#' + tableId).fadeIn(250); }
or would this be faster?
// shows the table and changes the image to up showTable = function(tableId){ $('#img' + tableId).replaceWith('some html'); $('#' + tableId).fadeIn(250); }
The replaceWith() method in jQuery is used to replace the selected elements with the new one. This method replaces the matched elements with the specified HTML elements. It returns the replaced elements. This method is similar to the replaceAll() method.
To set multiple attributes at once on an element: Add the attribute names and values to an object. Use the Object. keys() method to get an array of the object's keys.
The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element. When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.
$('#greatphoto').attr({ alt: 'Beijing Brush Seller', title: 'photo by Kelly Clark' });
Example taken from jQuery Documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With