Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change multiple attributes or replace entire html

Tags:

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); } 
like image 585
Timothy Ruhle Avatar asked Mar 01 '11 08:03

Timothy Ruhle


People also ask

How to replaceWith in jQuery?

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.

Can you set multiple attributes in Javascript?

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.

How to assign attribute value in jQuery?

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.


1 Answers

$('#greatphoto').attr({   alt: 'Beijing Brush Seller',   title: 'photo by Kelly Clark' }); 

Example taken from jQuery Documentation.

like image 72
yan.kun Avatar answered Oct 13 '22 11:10

yan.kun