Reputation: 442
Working in Firebug Console, but not from file.
Google Chrome - Uncaught TypeError: Object # has no method 'listAttributes'
Firefox - $(".div4").listAttributes is not a function
<script src='/jquery.js'></script>
<script src='jquery.listAttributes.js'></script>
<div class='div4' style='color:red;'>
</div>
<script>
$(".div4").listAttributes();
</script>
jquery.listAttributes.js:
if(jQuery) {
jQuery(document).ready(function() {
jQuery.fn.listAttributes = function(prefix) {
var list = [];
$(this).each(function() {
console.info(this);
var attributes = [];
for(var key in this.attributes) {
if(!isNaN(key)) {
if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
attributes.push(this.attributes[key].name);
}
}
}
list.push(attributes);
});
return (list.length > 1 ? list : list[0]);
}
});
}
Where is my mistake?
Upvotes: 0
Views: 593
Reputation: 630379
This code:
$(".div4").listAttributes();
Is running before document.ready
, but your plugin isn't defined until document.ready
, just remove the jQuery(document).ready(function() { });
wrapper from your plugin :)
Another note, the call to the plugin should be in a document.ready
handler, like this:
$(function() {
$(".div4").listAttributes();
});
This ensures that the .div4
elements are in the DOM and ready to go.
Upvotes: 4
Reputation: 943214
You are calling $(".div4").listAttributes();
inline in the script (so it is called when the browser parses that <script>
element) but you are assigning jQuery.fn.listAttributes
inside a ready
event so it doesn't exist until after the browser has finished parsing the entire document.
Upvotes: 1