Created
November 15, 2010 04:03
-
-
Save srhyne/700015 to your computer and use it in GitHub Desktop.
Using jQuery $.extend method to change DOM attributes..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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