Jump to content

jQuery: include and exclude


kayut

Recommended Posts

Hi guys,I have following task in front of me and I really don't know where should I start.In a HTML form I have two input boxes for urls:URL FilterInclude:Exclude:The task is as follow:Include google - will include all the domains with "google" in the url.Exclude "referer" - will exclude all the domains that contains "referer" in the url.Is there any jQuery solution for this?Many thanks

Link to comment
Share on other sites

By "include" and "exclude" what are you refering to?It makes sense in the context of a browser extension that should only run on certain websites but I can't imagine a similar situation with HTML forms.

Link to comment
Share on other sites

You can let them enter a comma-separated list, and break it up to exclude or include each one. You haven't really described what excluding or including actually means in your application (i.e. what you're using "the URL" for), but if your problem is letting them add multiple filters then tell them to separate them with a comma or a space and then split them up when you're processing the form.

Link to comment
Share on other sites

Say you have your list of URLs:

var urls = [	'http://google.com/',	'http://example.com/',	'http://example.com/google'];

Retrieve the contents of the two inputs (I'm assuming you know how to do that; if not, look up the .value property), and use .indexOf to see whether the string is contained in the URLs. Clone the array, then cycle through it removing all elements which do not contain the "include" value and do include to "exclude" value. You'll end up with something like this:

var urls = [	'http://google.com/',	'http://example.com/',	'http://example.com/google']; $('#include, #exclude').keyup(function () {	var include = $('#include').val(),		exclude = $('#exclude').val(),		newUrls = urls.clone(); 	for (var i = 0; i < newUrls.length; i++) {		if (newUrls[i].indexOf(include) === -1 || newUrls[i].indexOf(exclude) !== -1) {			newUrls.splice(i, 1);			i--;		}	}});

Untested. There are a number of improvements that could be made (such as only retrieving the .val() of the updated input, or looping backwards to remove the i--), so when you understand everything that is there, see what you can improve. EDIT: How do I do inline code?

Edited by callumacrae
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...