Sunday, December 16, 2012

Share the knowledge: Apache Solr: Accessing the Solr server using perl

Share the knowledge: Apache Solr: Accessing the Solr server using perl:     This module is used to interact with Apache Solr web application. Apache solr by default runs on 8983 port. Apache solr provides web int...

Tuesday, March 27, 2012

Get all input values by jQuery's serialize function

Often developers want to get a quick way to see the CGI parameters from a form And the good news is this can be done very easily in jQuery! Just a single line of code! $('[name=form]').serialize() Enojy!

How to get and set form element values with jQuery

How to get and set form element values with jQueryHow to get and set form element values with jQuery

Posted November 13th, 2008 in Javascript (Updated April 22nd, 2010)

jQuery is a Javascript framework which can simplify coding Javascript for a website and removes a lot of cross browser compatibility issues. This post looks at how to get and set the values of HTML form elements with jQuery.

Getting a form value with jQuery

To get a form value with jQuery use the val() function. Let's say we have a form like this, using an id for each form element:

1<input name="foo" id="foo" type="text">
2 
3<select name="foo" id="bar">
4    <option value="1">one</option>
5    <option value="2">two</option>
6    <option value="3">three</option>
7</select>

We can display an alert for the values of "foo" and "bar" as easily this:

1window.alert( $('#foo').val() );
2window.alert( $('#bar').val() );

If we're using the name only and not specifying an id, the jQuery to get the form values would be this:

1window.alert( $('[name=foo]').val() );
2window.alert( $('[name=bar]').val() );

If you have a group of radio buttons and want to get the selected button, the code is slightly different because they all have the same name. Using the above code examples will show the value for the first radio button on the form with that name. To find out the value of the checked one, do this instead:

HTML:

1<input type="radio" name="baz" value="x">
2<input type="radio" name="baz" value="y">
3<input type="radio" name="baz" value="z">

jQuery:

1window.alert($('input[name=baz]:checked').val());

Setting a form value with jQuery

You can set the form values with jQuery using the same val() function but passing it a new value instead. Using the same example forms above, you'd do this for the text input and select box:

1$('#foo').val('this is some example text');
2$('#bar').val('3');
3OR
4$('[name=foo]').val('this is some example text');
5$('[name=bar]').val('3');

Using the above for a radio button will change the actual value of the radio button rather than changing the one that is selected. To change the radio button that is selected you'd do this:

1$('input[name="baz"]')[0].checked = true;

[0] would set the first one checked, [1] would set the second one checked and so on.

Working demos

For working demos of the above examples (and more) please see my demos for getting and setting form values with jQuery, how to check and uncheck a checkbox with jQuery, and clear a form with jQuery posts.

Comments

Glad you liked it. Would you like to share?

Sharing this page …

Thanks! Close

Add New Comment

Showing 19 comments

blog comments powered by Disqus

Wednesday, March 21, 2012

Finding Max And Min Values with Versioned Documents

Finding Max And Min Values with Versioned Documents Credit: Amos King Problem You want to list the latest version numbers of a set of documents. Each document contains a field that represents the version of the document and a field representing which document that is a version of: { "document_id" : "mongoDB How-To", "author" : "Amos King", "content" : "...", "version" : 1.0 } We want to end up with a collection of document_ids and their largest version number: {"_id" : "mongoDB How To", "value" : 1.1} {"_id" : "Resume", "value" : 6} {"_id" : "Schema", "value" : 1} Solution Use the mapreduce database command. Emit each document_id and version in the map function, then use the reduce function to find the max version. 1. Map The map function is very simple. We use our common element between all versions as the key and the version as the value: map = function () { emit(this.document_id, this.version); } 2. Reduce The reduce function is also very simple but has a little bit of javascript magic. Math.max normally takes in any number of arguments(ie. Math.max(1,2,3) ), but we need to call it with an array. So we call Math.max with apply so that we can pass in an array of values to max. The apply breaks the array into individual arguments to pass to Math.max. The first argument to apply is the context in which we want to run; Math will do fine here. reduce = function (key, values) { return Math.max.apply(Math, values); } Finding the minimum value is as easy as replacing Math.max with Math.min. 3. Call the mapreduce command Now it's time to get our result set. We'll set the output collection name parameter to 'newest_versions' so that we'll have an appropriately named set to work with: > result = db.runCommand({ ... "mapreduce" : "documents", ... "map" : map, ... "reduce" : reduce, ... "out" : "newest_versions"}) Now, we query the 'newest_versions' collection. Each document is exactly what we're looking for: > db.newest_versions.find() {"_id" : "mongoDB How To", "value" : 1.1} {"_id" : "Resume", "value" : 6} {"_id" : "Schema", "value" : 1} Extras The Map and Reduce Functions can be rewritten slightly to return the Maximum and Minimum versions of each document. For the purpose of this example, the input collection is as follows: (The _id values have been truncated for brevity.) > db.documents.find() { "_id" : 1, "document_id" : "mongoDB How-To", "author" : "Amos King", "content" : "...", "version" : 1 } { "_id" : 2, "document_id" : "mongoDB How-To", "author" : "Amos King", "content" : "...", "version" : 1.1 } { "_id" : 3, "document_id" : "Resume", "author" : "Author", "content" : "...", "version" : 6 } { "_id" : 4, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 0.9 } { "_id" : 5, "document_id" : "Schema", "author" : "Someone Else", "content" : "...", "version" : 1 } > Map The new Map function emits documents containing the document_id, and "value" key containing a list of embedded documents, each containing the keys, "max" and "min". Both keys are initially set to be equal to the "version" key of the current document. Because there is only one document containing the "document_id" : "Resume", this output will not need to be reduced.

Monday, March 12, 2012

GettingStarted - Cassandra Wiki

GettingStarted - Cassandra Wiki: And now for the moment of truth, start up Cassandra by invoking bin/cassandra -f from the command line1. The service should start in the foreground and log gratuitously to standard-out. Assuming you don't see messages with scary words like "error", or "fatal", or anything that looks like a Java stack trace, then chances are you've succeeded.

Press "Control-C" to stop Cassandra.

If you start up Cassandra without "-f" option, it will run in background, so you need to kill the process to stop.

setup passphraseless ssh

setup passphraseless ssh check whether you can ssh to the localhost without a passphrase: $ ssh localhost If you cannot ssh to localhost without a passphrase, execute the following commands: $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

Sunday, March 11, 2012

Welcome to Apache™ Hadoop™!

Welcome to Apache™ Hadoop™!: The Apache™ Hadoop™ project develops open-source software for reliable, scalable, distributed computing.

The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using a simple programming model. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-avaiability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-availabile service on top of a cluster of computers, each of which may be prone to failures.