Skip to content

Instantly share code, notes, and snippets.

@srhyne
Created November 15, 2010 04:03
Show Gist options
  • Save srhyne/700015 to your computer and use it in GitHub Desktop.
Save srhyne/700015 to your computer and use it in GitHub Desktop.
Using jQuery $.extend method to change DOM attributes..
// 1. DOUBLE BAD!!
$("#myLink").attr("name","srhyne");
$("#myLink").attr("href","http://stephenrhyne.com";
$("myLink").html("stephenrhyne.com");
// 2. IN MY OPINION STILL VERY BAD..
$("#mylink")
.attr("name","srhyne")
.attr("href","http://stephenrhyne.com")
.html("stephenrhyne.com")
// 3. NOT AS UBER JQUERY DEVELOPER COOL BUT BETTER! (You don't need $ for everything!)
var myLink = $("#myLink")[0];
myLink.name = "srhyne";
myLink.href = "http://stephenrhyne.com";
myLink.html = ("stephenrhyne.com");
// 4. I link using just plain DOM scripting in #3. but I hate not being able to chain.
// I like this much better..
$.fn.domExtend = function(obj){
$.extend(this[0],obj);
return this;
};
$(function(){
$("#myLink").domExtend({
name : "srhyne",
href : "http://stephenhyne.com",
innerHTML : "stephenrhyne.com"
});
});
// 5. Of course this is the same as going like this..
var myLink = $("#myLink")[0],
changes = {
name : "srhyne",
href : "http://stephenhyne.com",
innerHTML : "stephenrhyne.com"
};
$.extend(myLink,changes);
// 4 AND 5 ARE acceptable to me.
//<a href="http://test.com" name="test" id="myLink" class="thisLink">Change ME!</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment