Skip to content

Instantly share code, notes, and snippets.

bookListItem = $('<li book-id=' + id + '><a href="book.html"><h2>' + title + '</h2><p>' + realText + '</p></a></li>');
bookListItem.click(function(){
book_id = $(this).attr("book-id");
localStorage.setItem("id", book_id);
});
// xml document
var xml = ('<section> ' +
' <enclosure url="a"/> ' +
' <other_tag> ' +
' <enclosure url="b"/>' +
' </other_tag> ' +
'</section> ' )
$(xml).find("enclosure").each(function(i) {
console.log( i.attr('url') );
<section>
<enclosure url="a" />
<other_tag>
<enclosure url="b" />
</other_tag>
</section>
$(xml).find("enclosure").each(function(i) {
console.log( i.attr('url') ); // -> TypeError: i.attr is not a function
});
$(xml).find("enclosure").each(function(i) {
console.log( $(this).attr('url') ); // note that I'm using $(this) and not i
});
var $enclosure_search = $(xml).find("enclosure")
// different values for similar calls
$enclosure_search // -> Object { 0: <enclosure>, 1: <enclosure>, length: 2, prevObject: Object, context: undefined, selector: "enclosure" }
$enclosure_search[0] // -> <enclosure url="a">
$($enclosure_search[0]) // -> Object { 0: <enclosure>, context: <enclosure>, length: 1 }
// Get the first enclosure tag explicitly - #attr() can no longer be called
$enclosure_search[0].attr('url') // -> TypeError: $enclosure_search[0].attr is not a function
// TODO: needs more jQuery
$( $(1)[0] + $(1 + 1)[0] + $(100).length ).first()[0] // -> 4
pre.code {
border: 1px solid #919191;
color: rgb(153, 58, 23);
padding: 2px;
padding-left: 10px;
border-radius: 2px;
}
@ahirschberg
ahirschberg / redundant_librifox.js
Last active August 29, 2015 14:17
some code in librifox that I refactored
// Initial code:
var xml = xhr.response;
var xmlDoc = $.parseXML( xml ); // xml variable is never referenced after this line
var newXML = $( xmlDoc ); // redundant form of above line, does nothing
var title = newXML.find( "title" ); // misleading, should be plural 'titles' (#find returns all title tags in the document)
var enclosure = newXML.find("enclosure"); // never used
var currTitle; // unnecessary
var currEnclosure; // unnecessary, never used
// Refactored into:
/// This is an excerpt from the app's search result generation code
/// entry is a book's json data
var book = new Book({'json': entry}); // -> Book object with keys: id, title, description, and json
bookCache[book.id] = book; // add book to cache with its id as a key
// Book id is in the link href: for example href="chapters.html?9396"
bookListItem = $('<li><a href="chapters.html?'+ book.id + '"><h2>' + book.title + '</h2><p>' + book.description + '</p></a></li>');