jQuery quickies

Written on 8 May 2011, 11:13pm

Tagged with: ,

1. Get all the text input fields or textareas having IDs starting with a pattern

$('input, textarea').filter('[id^='+fieldID+']').each(function() 
{
	var field_value = this.value;
	var field_id = this.id;
	//do things here...
}

2. Add options to a select (in the example below – add the Google supported languages)

//Get the Languages array
	var languages = google.language.Languages;

	//populate the languages select
	$.each(languages, function(key, value)
		{   
		$('#my_select').
		append($("<option></option>").
		attr("value",value).
		text(key)); 
		}
	);

(more…)

Did you know the formula of love? It’s
x^2+(y-sqrt(x^2))^2=1
Plotting this will result in a nicely hearth-shaped figure. The ultimate geek gift for his geek girlfriend!
I reproduced this plot using two methods: ColdFusion cfchart/cfchartseries/cfchartdata tags and a jQuery plotting library, called flot.

1. ColdFusion code

cflove.cfm (tested both on ColdFusion 8 and ColdFusion 9)

<cfchart format="flash" xaxistitle="X" yaxistitle="Y">
	<cfchartseries type="scatter">
		<cfloop index="x" from="-1" to="1" step="0.02">
			<cfset y1 = sqr(x^2)-sqr(1-x^2)>
			<cfchartdata item="#x#" value="#y1#">
		</cfloop>
	</cfchartseries>
	<cfchartseries type="scatter">
		<cfloop index="x" from="-1" to="1" step="0.02">
			<cfset y2 = sqr(x^2)+sqr(1-x^2)>
			<cfchartdata item="#x#" value="#y2#">
		</cfloop>
	</cfchartseries>
</cfchart>

You will notice that the formula of love is composed by two curves:
-the upper part of the hearth: y = sqrt(x^2) – sqrt(1-x^2)
-the lower part of the hearth: y = sqrt(x^2) + sqrt(1-x^2)
The values of x are between -1 and 1.

Go ahead and load this file on your ColdFusion server. It will nicely draw the following image: (more…)