Does jQuery strip some html elements from a string when using .html()?

After a few quick tests it seems do me that this behavior isn’t caused by jQuery but instead by the browser. As you can easily verify yourself (DEMO http://jsbin.com/ocupa3) var data = “<html><head><title>Untitled Document</title></head><body><p>test</p></body></html>”; data = $(‘<div/>’).html(data); alert(data.html()); yields different results in different browsers Opera 10.10 <HEAD><TITLE>Untitled Document</TITLE></HEAD><P>test</P> FF 3.6 <title>Untitled Document</title><p>test</p> IE6 <P>test</P> so … Read more

jQuery – How to get all styles/css (defined within internal/external document) with HTML of an element

outerHTML (not sure, you need it — just in case) Limitations: CSSOM is used and stylesheets should be from the same origin. function getElementChildrenAndStyles(selector) { var html = $(selector).outerHTML(); selector = selector.split(“,”).map(function(subselector){ return subselector + “,” + subselector + ” *”; }).join(“,”); elts = $(selector); var rulesUsed = []; // main part: walking through all … Read more

Truncate paragraph first 100 character and hide rest content of paragraph to show/hide rest contenct with more/less link

Fiddle: http://jsfiddle.net/iambriansreed/bjdSF/ jQuery: jQuery(function(){ var minimized_elements = $(‘p.minimize’); var minimize_character_count = 100; minimized_elements.each(function(){ var t = $(this).text(); if(t.length < minimize_character_count ) return; $(this).html( t.slice(0,minimize_character_count )+'<span>… </span><a href=”#” class=”more”>More</a>’+ ‘<span style=”display:none;”>’+ t.slice(minimize_character_count ,t.length)+’ <a href=”#” class=”less”>Less</a></span>’ ); }); $(‘a.more’, minimized_elements).click(function(event){ event.preventDefault(); $(this).hide().prev().hide(); $(this).next().show(); }); $(‘a.less’, minimized_elements).click(function(event){ event.preventDefault(); $(this).parent().hide().prev().show().prev().show(); }); });​

Find and replace nth occurrence of [bracketed] expression in string

Here is another possible solution. You can pass the string.replace function a function to determine what the replacement value should be. The function will be passed three arguments. The first argument is the matching text, the second argument is the position within the original string, and the third argument is the original string. The following … Read more

Grouping JSON by values

Assume , the JSON output is outJSON = [ { team: “TeamA”, name: “Ahmed”, field3:”val3″ }, { team: “TeamB”, name: “Ahmed”, field3:”val43″ }, { team: “TeamA”, name: “Ahmed”, field3:”val55″ }, ] Then see the groupBy function in the DEMO below: DEMO : outJSON = [{ team: “TeamA”, name: “Ahmed”, field3: “val3” }, { team: “TeamB”, … Read more

Looping through Markers with Google Maps API v3 Problem

You are having a very common closure problem in the following loop: for(x in locations){ console.log(x); infowindow[x] = new google.maps.InfoWindow({content: x}); marker[x] = new google.maps.Marker({title:locations[x][0],map:map,position:locations[x][2]}); google.maps.event.addListener(marker[x], ‘click’, function() {infowindow[x].open(map,marker[x]);}); } Variables enclosed in a closure share the same single environment, so by the time the click callbacks are executed, the loop has run its course … Read more