Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Tuesday, April 16, 2013

How to pass date in querystring on jquery

The post is about jquery multiple date handled in querystring in ajax method. Here, we have used array to send the date through querystring. This code will reduce our work weight-age. Just need to form a array with unlimited index with unlimited dates.

Friday, January 6, 2012

Javascript: Remove duplicates from Array

There is now an array prototype function for removing duplicates. I suggest you use the prototype function instead of the functions below. It is the way it should have been done in the first place and is faster and moreover the most correct way to do it.
Array.prototype.unique = function () {
 var r = new Array();
 o:for(var i = 0, n = this.length; i < n; i++)
  for(var x = 0, y = r.length; x < y; x++)
    continue o;
  r[r.length] = this[i];
 return r;
You can now utilize the unique function like this:
var arr = [1,2,2,3,3,4,5,6,2,3,7,8,5,9];
var unique = arr.unique();
The result will be [1,2,3,4,5,6,7,8,9].

The first one does what you’ll expect, it’ll regard the first encountered entry as the original and all subsequent entries as duplicates.
function unique(a)
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
      for(var x = 0, y = r.length; x < y; x++)
         if(r[x]==a[i]) continue o;
      r[r.length] = a[i];
   return r;
If we pass the following array [1, 2, 3, 1, 4, 5] to the function the result will be [1, 2, 3, 4, 5]. The second variant returns different results. It will regard the last encountered duplicate as the original. This may be desirable in certain situations.
function unique(a)
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++) {
      for(var x = i + 1 ; x < n; x++)
         if(a[x]==a[i]) continue o;
      r[r.length] = a[i];
   return r;
The output in this case will be [2, 3, 1, 4, 5]. You can easily adapt the function to make it a prototype function of an Array object. If people don’t know how to do it just ask, i’ll add it. Hope this helps someone out!

Tuesday, January 3, 2012

Autocompletion with Scriptaculous and Ajax

This tutorial supercedes my previous turtorial on autocompletion by adding further tips and tricks to make your autocompletion a much better experience for your site users The tutorial is structured in the following way:
  1. Introduction: What is autocompletion and why use it?
  2. How it works.
  3. Getting scriptaculous and building the form.
  4. Building the autocomplete server.
  5. Passing back an id to our form.

1. What is autocompletion and why use it?

I predict that most people will want to skip this section and get straight to the action, so I will keep it brief. Autocompletion is a means of helping your users when filling in forms. The most common application of autocomplete is found in your web browser’s address bar; as you type the bar makes suggestions of URLs based on previous pages visited. In our example we will query a database of musical artists and give our users suggestions as they type in an artist name field.

2. How it works.

As a user types in a specific input field on a form, on each key up event, we use JavaScript to call a script in the background using Ajax (XMLHttpRequest) features built into the Scriptaculous library. Our form will pass the currently typed text to this script and the script will return a list of suggestions which will be dynamically placed in our page. The important thing is that our user can select one of these items by mouse-click or key-stroke to populate the form field instantly.

3. Getting scriptaculous and building the form.

This implementation of autocomplete requires 3 components:
  1. The Scriptculous libraries;
  2. An HTML form with a single field;
  3. An autocomplete server script
I use PHP as my language of choice for the autocomplete server script, but I have tried to keep this tutorial abstract enough to allow you to use your language of choice. The server script is the only thing that changes for different server-side scripting languages. Download the latest version of the Scriptaculous JavaScript Libraries and include them in the head of a new HTML document as below.

Don’t forget to change the paths to match your directory structure:

<script type="text/javascript" src="lib/prototype.js"></script>
<script type="text/javascript" src="src/scriptaculous.js"></script>

Now add a form to your HTML page’s body. In this form we are going to add a field that will take our search term (with both id and name attributes as “searchterm”) and an empty div with an id of “hint”. This empty div acts as a holder for our hints when they are generated. You should have some HTML looking like this:

<input type="text" name="searchterm" id="searchterm" />
<div id="hint"></div>

All we need now is to initiate a new call to Scriptaculous’ built in Ajax.Autocomplete method below our HTML code for the autocompleter – like this:

<script type="text/javascript">
new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”);

The Ajax.Autocompleter takes 3 arguments: The element supplying the search string, the element to show suggestions in and the server script that will generate our suggestions. With that in place we can write the server script.

4. Building the autocomplete server.

The following server is written in PHP connecting to a MySQL database but you can your language of choice and even a flat text file or XML file for the suggestion database. My database table, “Artists”, is simple – it comprises a unique id field and an artist “name”.
As the Autocompleter sends the value of a form field as a POST variable, we can use this in our server script’s query againstthe database. All we need to do is output an unordered list for the autocompleter. Scriptaculous does the leg work of inserting the latest list into our hint box everytime the user makes a keystroke. Here is my server code in PHP

// don't forget your database connection here.
echo "<ul>";
$sql = "SELECT * FROM artists WHERE name LIKE '%" . $_POST['searchterm'] . "%'";
$rs = mysql_query($sql);
while($data = mysql_fetch_assoc($rs)) {
echo "<li id=\"" . $data['id'] . "\">" . $data['name'] . "</li>";
echo "</ul>";

Note that I have given each list item an id that is the same as that of the record. We will use this shortly for a more advanced technique. But for now we have all we need for a basic autocompleter.

5. Passing back an id to our form

Once a user has selected an item from the autocompleter, the string is entered into the textfield. Often, for the sake of efficiency and accuracy it is better to deal with the id of the item than the string name when it is passed on to the next page. To do this we can use some extra functionality of Ajax.Autocompleter along with some JavaScript that will pass the id to a hidden field in our form.

The first thing we need to add to our form is a hidden field with an id that we can reference:

<input type="hidden" id="artist_id" name="artist_id" />

Now we can use an extra piece of functionality built into Ajax.Autocompleter that allows us to pick up the id that we included in our list item from the server script. This is an extra option on our autocompleter call:

new Ajax.Autocompleter(”searchterm”,”hint”,”server.php”, {afterUpdateElement : getSelectedId});

We are telling our autocompleter to run a function called getSelectedId when an item is selected. All we need to do now is write this function to get the id and inject it into our hidden field. Insert the function below the

function getSelectedId(text, li) {

And that’s all there is to it. Of course you can be as clever as you want with your autocomplete server script. Try using the LIMIT clause in your SQL to make it run faster. Make your queries more advanced to bring back more specific results. And don’t forget the fun you can have with CSS on your hint div and the list of suggestions that it displays. If you have any implementations of autocompletion using this tutorial, please post them in the comments here.

How to get the javascript objects in html body tag



function jsAdd()

var temp=document.getElementById("count");
var values=temp.value;

var parent = document.getElementById("family");
div = document.createElement("div");
var tbl=div.appendChild(document.createElement("table"));
var tb=tbl.appendChild(document.createElement("tbody"));
var tr=tb.appendChild(document.createElement("tr"));
var td=tr.appendChild(document.createElement("td"));

var oSelect=document.createElement("select");

var option1 = document.createElement("option");
var t = document.createTextNode("Parent");

var option2 = document.createElement("option");
var t = document.createTextNode("Spose");

var option3 = document.createElement("option");
var t = document.createTextNode("Chaild");
option3.setAttribute("value", "Chaild");

var option4 = document.createElement("option");
var t = document.createTextNode("Other");
option4.setAttribute("value", "Other");
var fname=document.createElement("input");
fname.value="Enter name";"name"+values;
var age=document.createElement("input");
age.value="Enter Age";"age"+values;


function count()

<body onload="count()">
<form action="./submit.jsp" method="post">
<input type="hidden" value="0" id="count" name="count">
<div name="family" id="family">
<input type="button" value="add" onCLick="jsAdd()">
<BR><br><input type="submit">



<body >
<tr><td>Relation Ship</td><td>NAME</td><td>Age</td></tr>

int count=Integer.parseInt(""+request.getParameter("count"));
for(int i=0; i<count;i++){%>
<% } %>


Wednesday, October 12, 2011

Creating an Image Watermark

Well, this is my last javascript post for a bit, seeing as this is just an offshoot of my last post Javascript: Dynamically Altering CSS Properties. In this post, we’ll go over creating a simple Javascript watermark for a textbox using an image. If you are curious how to create a watermark with just text, read my other post on how to create a textbox text watermark. A textbox watermark can help encourage its use and reduce confusion for your users. I typically employ a watermark for textboxes that exist outside of a form (they only gather one piece of needed information such as a zip code as opposed to an entire address).
So either in an external Javascript file (which is called the same way as an external CSS file), or in the head of your HTML inside a <script> tag, include the following javascript.
The Javascript:
function hideWatermark(theID)
    var element = document.getElementById(theID); = 'none'; = 'white';
function showWatermark(theID)
    var element = document.getElementById(theID);
    if (element.value.length == 0) = 'url(\'IMAGE_LOCATION_HERE\')';
    else = 'white';
On focus (when the user clicks on the textbox) the hideWatermark function is called. The javascript takes in a string of the textbox ID. It then sets the background image to none, and the background color to white. On blur (when the user clicks away from the textbox, putting focus on any other element), the showWatermark function is called. This javascript also takes in the same ID. It then checks to see if the content of the textbox is empty, if it is, it puts the background image of the watermark back in. If it is not empty, then it keeps the background color as white.
Here is the HTML that goes with the JS:
And the CSS:
   background: white url('IMAGE_LOCATION_HERE') no-repeat center center;
The image you create can say or imply whatever you want. As an estimate, try to keep the image no more than about 15 pixels high or it won’t fit in standard sized textboxes. And that’s it. A beautiful textbox watermark just for you and your users! Comments? Questions?