This is more for my reference and hopefully helps others working with JQuery.
-
Getting and Setting the innerHTML of an element
var content = $("#id").html();
$("#id").html("Some HTML");
-
Getting and Setting the text (with out any HTML) of an element
var txt = $("#id").text();
$("#id").text("Some Text");
-
Removing content from an element
$("#id").empty();
-
Getting and Setting style properties
var displayValue = $("#id").css("display");
$("#id").css("display", "block");
-
Hiding and Displaying elements
// Toggles an element's visibility
$("#id").toggle();
$("#id").hide();
// Sets the display style property to block and not inline
$("#id").show();
-
Registering to events such as keyup
$("#id").keyup(function(){
alert($(this).val());
});
-
Dealing with CSS classes
// Adds the specified class to the element
$("#id").addClass("showme");
// Multiple classes should be separated by spaces
$("#id").addClass("showme1 showme2");
// Removes the specified class
$("#id").removeClass("hideme");
// Multiple classes should be separated by spaces
$("#id").removeClass("hideme1 hideme2");
// Removes all the classes
$("#id").removeClass();
-
Getting and Setting values of form elements
// For input boxes
$("#inputbox").val(); // Returns the text box value
$("#inputbox").val("set some value");
// For select boxes
$("#selectbox").val(); // Returns the selected value
// To get the text of the selected option
$("#selectbox :selected").text();
// Programatically selects a value or multiple values
$("#singleselect").val("123");
$("#multipleselect").val(["12", "234"]);
// For checkboxes and radio buttons
$("#chkId").val(); // Returns value whether the check box is checked or not
// Returns true when the check box is checked
$("#chkId").attr("checked");
// Programatically checks the check box
$("#chkId").attr("checked", "checked");
I feel that an important advantage of using JQuery is its ability to separate behavior from structure. Consider the case where you want to trigger a JavaScript method when a link is clicked. A common implementation would be:
Even though this mixing of behavior (JavaScript) and structure (HTML) looks straight forward, it can become a maintenance issue in large projects. This is where JQuery shines. Using JQuery, the above code can be rewritten in a cleaner way:
Now, consider a slightly trickier case where context specific parameters need to be passed to the JavaScript method:
Here is the refactored version using JQuery:
JQuery’s form plugin provides several convenient methods for clearing/resetting form element values. However, the clearForm method does not reset hidden fields. Here is a simple implementation for clearing hidden fields:
jQuery.fn.clearHiddenFields = function() {
return this.each(function(){
$("input[type='hidden']", this).each(function(){
this.value = '';
});
});
};
Invoking this method is very straightforward:
$("#yourformid").clearHiddenFields();
or simply:
$("form").clearHiddenFields();
Here is some code to send a message to a JMS queue using Spring:
ConnectionFactory connectionFactory = // Look up connectionfactory from JNDI
Queue queue = // Look up distributed queue from JNDI
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.send(queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
// Set the message content
message.setObject(new Object());
return message;
} }
);
As I was googling, I came across this in the search results:
Interesting idea from google.
I was recently in a discussion where I shared that ORM might not be a good tool for applications whose main focus is to:
- provide UI for editing data in the database tables
- simply generate reports of the database data
I feel that ORM is a great tool when you have a solid domain model but from what I have seen, you really don’t need a domain model to “interface” database tables.
Any thoughts??