Search This Blog

Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Sunday, September 7, 2014

Jquery : How to get full html string including the selected element itself with jQuery's $.html()

How to get full html string including the selected element itself with jQuery's $.html()

Sometimes you need to get the selected element's html as well when using .html() function. To make it more clear what I mean, consider you have this HTML markup:
 
Some content
More content
And you need to get not only
 
 
Some con... but
Some con...
Here is the code to get jQuery selector's HTML including its own:
 

var html = $('
').append($('#top').clone()).remove().html();

Here we are:

  • Cloning selected div
  • Creating a new div DOM object and appending created clone of the div
  • Then getting the contents of wrapped div (which would give us all HTML)
  • Finally removing the created DOM object, so it does not clutter our DOM tree.
  • This is a little trick you can use to select self HTML with jQuery's .html() function. But if you can you should surround your markup with some dummy div and avoid this workaround all together. This would be much faster since jQuery would not need to do all the DOM manipulations.

jQuery : Remove (script) tags from a string of html

jQuery : Remove (script) tags from a string of html 


I'm in the middle of writing a bookmarklet to screen scrape web pages. My back-end piece is having trouble parsing pages with lots of script tags. Since I don't actually need the script tags, I thought I would remove them before it even gets to the back-end. Here is how to grab the current page HTML and strip some tags from it, without changing the DOM the user is actually viewing.
 

//Remove (script) tags from a string of html 
var page_content = $("html").clone().find("script,noscript,style").remove().end().html();  

// Remove SCRIPT - LINKS TAG From Ajax Response 

    

That's it! I then POST the html content to the back-end with an ajax call. The key bit is the clone(), which copies the entire page so that we can remove element from the copy and not without removing it from the UI the users if viewing.


Wednesday, April 30, 2014

Jquery Ui DatePicker Tip: 1. Allow to Select only current and previous date 2. Allow to select date from two days later from current date




I had requirement for a datepicker  



1. Allow to Select only current and previous date

Allow to select only current date and previous date. And exclude Sunday from selection.
So if todays is Monday than enabled date should be last Saturday and today (Monday).



2. Allow to select date from two days later from current date

Allow to select date from two days later from current date . And exclude Sunday from selection.
So if today is Friday then date selection should start on next Monday. And if todays is Monday then date selection should start on next Wednesday.  

Here Is The Script




 
1. Allow to Select only current and previous date
Date1 :
2. Allow to select date from two days later from current date
Date2 :

Contact Form

Name

Email *

Message *