Posts Tagged ‘jQuery’

Returning HTML to jQuery with Coldfusion

Thursday, January 7th, 2010

I’ve spent the last couple months building an application that is very heavy on the front end with jQuery and reliant on Coldfusion and MySql for data processing and storage.While I’ve been using jQuery for some time now to pull off little cool things in more traditional server-request based applications, this project is really the first that I’ve worked on that has required me to really dig into what jQuery can really do.

Since I work with my wife, who also happens to be Art Director where we work, a position slightly higher than my own, I have to take very special care to make sure my code is easily editable on the CSS and HTML end.  I’ve found that, while manipulating HTML in the DOM with jQuery is incredibly simple, the format in which it has to be written is slightly unreadable and written in a portion of the javascript that the designer may not be able to easily locate when an edit needs to take place.

Let’s take a look at how jQuery would add a table to the DOM using the html() method.

$("#mydiv").html("<table class='mytable'><tr class='header'><td>Header 1 Content</td><td>Header 2 Content</td></tr><tr class='content'><td>Content 1</td><td>Content 2</td></tr></table>");

<div id="mydiv"></div>

(more…)

jQuery to add textfield labels

Friday, December 25th, 2009

In the world of web aesthetics a nice principle to follow is the less clutter the better. Traditional forms have contained a label and then a text input usually. More and more we’re beginning to see the labels being placed inside the text field until the user selects them, at which point they can type whatever. The reason we may be seeing it more often is because jQuery, as usual makes what is otherwise a fairly time-consuming task and makes it simple.  To get an idea of what I’m talking about you can see a demo here.  I checked the demo.  I’m pretty sure that the tooltips work fine in Firefox, but are incorrectly positioned in Safari, and I don’t even want to know for IE.  I’m sure this is no fault of the plugin, but that there is just some conflict in the plugin CSS and my demo template CSS.  I just really didn’t feel like figuring it out.  On top of all that, if you don’t have Firefox to view the demo with shame on you

Let’s think about what it is we are wanting to accomplish.  Generally we want a form with no labels.  Instead our labels are a prefilled value inside the form field.  However, when a field with one of these prefilled labels is clicked or tabbed to we want to empty the field and ready it for typing.  Next if new data is entered the data should stay in the field, and if no data is entered the field should return to the prefilled label.  Lastly, if the user focuses a field that they have already entered data into we want to make sure that we don’t empty the field, but instead just select the entered data and ready it for editing.  One last thing.  Because our field label disappears when the field is selected I felt that it made sense to make sure that the user doesn’t forget what they are typing into the field.  To accomplish this I used the jQuery Tools tooltip plugin.  I won’t explain this too much, but for this demo I pretty much just copied and pasted their demo code from their site.  Nonetheless, this is a really powerful and flexible plugin, and on top of everything really looks great.

On to the code.  First we’ll take a look at the HTML.

The HTML

<!-- div that will show our tooltip -->
<div id="mytooltip">&nbsp;</div>

<!-- form with labels in the field label in the 'id' and tooltip in the 'title' -->
<form name="myForm" id="myFormId" method="post" action="#">
    <input type="text" name="firstname" id="Your First Name" value="" title="Enter Your First Name" /><br /><br />
    <input type="text" name="lastname" id="Your Last Name" value="" title="Enter Your Last Name" /><br /><br />
    <input type="text" name="mycolor" id="Your Favorite Color" value="" title="Enter Your Favorite Color" /><br /><br />
    <input type="text" name="mycat" id="Your Favorite Cat" value="" title="Enter Your Favorite Cat" /><br /><br />
    <input type="submit" name="mySubmit" />
</form>

(more…)

Disabling a form with jQuery

Tuesday, December 15th, 2009

Sometimes we need to disable an entire form based upon user input. The example I thought of was in the case of asking how a person wants to pay for something. So for example, if a user wishes to pay for their product with a check we don’t need a form, but if they wish to pay with a credit card then we need to give the user a form to fill in their credit card information. You can see an example of this here.

I would normally just not show the form unless the user chose to pay with credit card.  In my opinion this is less intimidating to the user and keeps them scrolling as little as possible.  Nonetheless, here we’re just going to disable it if the user is paying with check, and enable it if the user is paying with credit card.  So let’s take a look at the HTML.

(more…)

Binding & Unbinding jQuery Event Listeners

Friday, December 4th, 2009

I had a little issue with jQuery yesterday evening.  The problem I was running into was running an event on an element created by jQuery.  Let me explain.  If I create a button in my HTML, and in jQuery write a $(“.button”).click(function(){//append a ’1′ to some element}); method, each time I click the button I will get a single 1 on the screen that has been added to the existing HTML.  For example, if I click my button five times, I will end up with ’11111′ being output the the page.  Wonderful.  This is how it’s supposed to work.  Click here to see a demo of this.  My HTML and jQuery look like the following for this example.

The HTML

<body>

<!— CREATE A BUTTON TO CLICK TO INITIALIZE OUR EVENT —>
<span class=”runscript”>RUN</span><br /><br /><br />

<!— SET AN EMPTY DIV TO BE POPULATED WITH CONTENT VIA JQUERY —>
<div class=”container”></div>

</body>

The jQuery

//once the document loads run our script
$(document).ready(function()
{

//bind an onclick listener to our RUN button
$(“.runscript”).click(function()
{

//run the addData() method
addData();

});

});

addData = function()
{

//append the word ‘output’ to our container HTML
$(“.container”).append(“Output <br />”);

}

(more…)

Adding DOM elements with jQuery

Sunday, November 29th, 2009

Adding DOM elements with jQuery is super simple and this post is a little more than what the title would lead one to believe.  So let me elaborate.  What I want to show here is a way to add DOM elements to a page with jQuery that falls into a certain slot of the available child elements.  More specifically choosing the index in which to insert your data in an array of children elements.  Check out a demo here just so we’re all on the same page of what is being done.

We’ll be working with three files for this demo.  First is our html (add_child_dom.html), our javascript (add_child_dom.js), and our css (add_child_dom.css).  The file paths here assume that all of these files live in the same directory.  If your files do not you will need to change the paths.  Let’s take a look at our html page first.

The HTML (add_dom_child.html)

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Add Child to DOM</title>

<!–load in the jquery library–>
<script src=”jquery.js” type=”text/javascript”></script>

<!–load in the js file that will focus our chosen form field–>
<script src=”focusField.js” type=”text/javascript”></script>

<!–load in the js file that will determine and manipulate the dom–>
<script src=”add_dom_child.js” type=”text/javascript”></script>

<!–load in the css file to set our styles–>
<link href=”add_dom_child.css” type=”text/css” rel=”stylesheet”/>
</head>

<body>
<div id=”parent”>
<p>This is original child 1 of div ‘parent’.</p>
<p>This is original child 2 of div ‘parent’.</p>
<p>This is original child 3 of div ‘parent’.</p>
<p>This is original child 4 of div ‘parent’.</p>
<span class=”button_demo”>ADD</span><br /><br />
</div>

<div>
Message to insert:
<input type=”text” name=”message” id=”focusField” /><br /><br /><br />

Insert into slot number:
<input type=”text” name=”slotNum” size=”3″ />
</div>
</body>
</html>

(more…)

Adding form focus with jQuery

Sunday, November 29th, 2009

It’s fairly standard practice to set a form field to focus on page load.  This allows the user to immediately start typing, as opposed to having to select a form field before proceeding.  See a demo of a simple login screen here.  Notice that the username field focuses once the page has loaded.  We have pretty simple HTML for this so let’s take a look at it first.

The HTML (form_focus.html)

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Form focus with jQuery</title>

<!–load in the jquery library–>
<script src=”js/jquery.js” type=”text/javascript”></script>

<!–load in the form focus js file–>
<script src=”js/focusField.js” type=”text/javascript”></script>
</head>

<body>
Username:
<input type=”text” name=”username” id=”focusField” /><br /><br />
Password:
<input type=”password” name=”password” />
</body>
</html>

(more…)

Creating a jQuery AJAX form with Coldfusion

Friday, November 20th, 2009

Although submitting a form via ajax is probably not a good idea in many cases, there are some cases where it might serve a purpose.  Forms on the web seem to be trending towards simplicity.  In the case that you’re working with a very simple form, like the one I’m using here (name, email, and comments), using a form that submits with AJAX as opposed to running through the server might be useful.  I’ve personally never used this, but was playing around with it and decided to post what I have in case anyone else is interested.  Pushing forward then.

This is what our form will do.  A user fills it out and clicks submit.  The form submits with AJAX (no page refresh) to a coldfusion component.  From there the data is stored in a database and emailed to the owner of the form.  This really isn’t that complicated, but several files are involved so I’ll run through those here.  Also for the sake of simplicity I’ve got all of our files in the same directory, but for the sake of organization you may consider a different directory structure.

1. formHTML.cfm (contains the html form)

2. formCFC.cfc (contains the methods to record data to database and email data)

3. formJS.js (contains the jquery to submit the form and return our success message)

(more…)

Selecting radio buttons with clickable text and jquery

Saturday, October 31st, 2009

Nothing frustrates me more than having to hone in my mouse pointer on a tiny radio button. That’s why many web forms today are allowing the area around the radio button to be clickable. I personally believe that if a radio button and its label is isolated in its own td or div tag the entire div or td should be clickable. A more common approach to this issue is making the label clickable.

Let’s take this example

Screen shot 2009-10-31 at 1.29.24 PM

The idea is that when a user clicks ’1 to 24′ the first radio button is selected, when ’25 to 99′ is clicked the second radio button is selected, and so on.  As usual to pull this off we need three resources: some HTML, CSS, and javascript (jQuery since we want this to be easy, right!).

(more…)

Creating a simple sliding navigation with jQuery

Friday, October 30th, 2009

As we know, jQuery not only makes things once considered impossible on the web possible, but makes those things pretty easy to do. I just built a sliding navigation for pukkared.com portfolio and thought I’d share here. Pulling this off requires both CSS and javascript knowledge. I’ll do my best to explain the CSS involved, but maybe Adri can follow up on this post with some more detail on why it works. I’ll also note that practically this navigation would be dynamically driven, but for the sake of simplicity I’m going to hard code the navigation. Maybe I’ll follow this up with a post on outputting your navigation from your database.

(more…)

jQuery and Coldfusion Builder

Friday, October 23rd, 2009

I just noticed that in the Coldfusion Builder preferences there is an option to code hint jQuery. Mine was not activated by default, but can be by going to preferences – HTML – Editors – Javascript – Code Assist. In the dialog is a list of javascript to choose from. Just make sure that the jQuery 1.3 box is checked to fulfill all of your jQuery code hinting desires.