Developers Archive for the 'Javascripts' Category

Color Change onClick

Color Change onClick Wednesday, March 19th, 2008

This a code sniplet for showing a shirt size selection by color change on mouse click in the size

<script language=”javascript”>

function set_selection(divid) {

document.getElementById(divid).style.backgroundColor = “red”;

document.getElementById(divid).style.color = “white”;

for(i=1;i<6;i++) {

if(i != divid) {

document.getElementById(i).style.backgroundColor = “white”; document.getElementById(i).style.color = “black”; }

}

} </script>

<table>

<tr> <th colspan=”5″>Select your Shirt Size</th> </tr>

<tr>

<td><div id=1 onclick=”set_selection(1)” style=”border: solid black 1px;width:35px;text-align:center”><b>S</div></td>

<td><div id=2 onclick=”set_selection(2)” style=”border: solid black 1px;width:35px;text-align:center”><b>M</div></td>

<td><div id=3 onclick=”set_selection(3)” style=”border: solid black 1px;width:35px;text-align:center”><b>L</div></td>

<td><div id=4 onclick=”set_selection(4)” style=”border: solid black 1px;width:35px;text-align:center”><b>XL</div></td>

<td><div id=5 onclick=”set_selection(5)” style=”border: solid black 1px;width:35px;text-align:center”><b>2XL</div></td> </tr>

</table>

auto-complete feature using jQuery

auto-complete feature using jQuery Tuesday, March 18th, 2008

Here we are going to discuss about implement a auto-complete feature using jQuery

<script type=”text/javascript” src=”jquery.js” mce_src=”jquery.js”></script>

<script type=”text/javascript” src=”dimensions.js” mce_src=”dimensions.js”></script>

<script type=”text/javascript” src=”autocomplete.js” mce_src=”autocomplete.js”></script>

Now we need to call the function that will bring life to our auto-complete field - setAutoComplete.

<script type=”text/javascript”>

$(function(){

    setAutoComplete(”searchField”, “results”, “autocomplete.php?part=”);

});

</script>

The call to setAutoComplete is inside a jQuery special code that is only executed when the DOM is ready, or in other words, when all the code is already loaded. The setAutoComplete function takes 3 parameters:

the id of the input field
the id of the div that will hold the returned data
the URL of the remote script that will process the request
Be aware that the URL should reflect your remote script and that it will be combined with the text typed in the input field.

Now we include our stylesheet to define the look of the elements. We need to define the styles of the div that will contain the results, they include two classes for the selected and unselected items. Take a look at the CSS file linked at the end of this post. There is also a style for the input field.

<link rel=”stylesheet” href=”autocomplete.css” mce_href=”autocomplete.css” type=”text/css”>

To finish the client side part now we need to define the code of the input field as follows:

<label for=”searchField”>Colors: </label>

<input type=”text” id=”searchField” name=”searchField”>

We are almost there! Now that the client side is finished it’s time for the server script. Here is an example of a script that try to match colors. I’m using PHP but you can use whatever language you prefer for the job, of course. You just need to return the results as an array encoded as JSON.

// define the colors array

$colors = array(’black’, ‘blue’, ‘brown’, ‘green’, ‘grey’,

                ‘gold’, ‘navy’ , ‘orange’, ‘pink’, ’silver’,

                ‘violet’, ‘yellow’, ‘red’);

 

// check the parameter

if(isset($_GET[’part’]) and $_GET[’part’] != ‘’)

{

        // initialize the results array

        $results = array();

 

        // search colors

        foreach($colors as $color)

        {

               // if it starts with ‘part’ add to results

               if( strpos($color, $_GET[’part’]) === 0 ){

                       $results[] = $color;

               }

        }

 

        // return the array as json

        echo json_encode($results);

 

        // or return using Zend_Json class

        // require_once(’Zend/Json/Encoder.php’);

        // echo Zend_Json_Encoder::encode($results);

}

Done! As said before, we return the data as an array encoded as JSON. If you are running PHP >= 5.2.0 or the json extension you simply use json_encode for the job. Another option is to use Zend_Json_Encoder from the Zend Framework!
 

 

Audio Player using Javascript with play and pause on mouse over and mouse out

Audio Player using Javascript with play and pause on mouse over and mouse out Friday, March 14th, 2008

The following javascript code will play , pause and stop the audio player on button click. To make it play, pause and resume on mouse over and mouse out you can call the appropriate function on the mouse over and mouse out of any html element like div, td, p etc. The code is as follows

<body onload=”init()”>
<script>
 function init()
 {
     document.getElementById(’wmpPlayer’).innerHTML = wmpCreate(’Alanis.mp3′);
 }
 function wmpCreate(url) {

    var str = “”;

    if (navigator.appName == “Netscape”) {
         // create it for FF.
         str = ‘<object id=”player” type=”application/x-ms-wmp” data=”‘+url+’” width=”1″ height=”1″>’;
         str += ‘<param name=”URL” value=”‘+url+’”>’;
         str += ‘<param name=”uiMode” value=”none”>’;
         str += ‘<param name=”autostart” value=”0″>’;
         str += ‘<param name=”hidden” value=”true”>’;
         str += ‘<param name=”pluginspage” value=”http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx”>’;           
         str += ‘</object>’;
    }
    else
    {
          str = ‘<embed NAME=”player” src=”‘+url+’” mce_src=”‘+url+’” MASTERSOUND LOOP=”false” AUTOSTART=”false” AUTOREWIND=”false” HIDDEN=”true” WIDTH=”0″ HEIGHT=”0″>’;
    }

    return str;

}
</script>

function player(toDo,toWhat)
{
     if (navigator.appName == ‘Netscape’)
       {
          var wmp = document.getElementById(toWhat);
          if(doWhat == ‘play’)
               wmp.controls.play();
          else if(doWhat == ‘pause’)
               wmp.controls.pause();
          else if(doWhat == ’stop’)
               wmp.controls.pause();    
       }
       else
       {
          var A = eval(’document.’+toWhat);
          if (A != null){
               if (document.M == null)
               {
                    document.M = false; var m;
                    for(m in A)
                    {
                         if (m == “ActiveMovie”)
                         {
                              document.M = true; break;
                         }
                    }
               }
               if (document.M)
               {
                    if (doWhat==’stop’) A.pause();
                    else
                    {
                         //A.SelectionStart = 0;
                         A.play();
                    }
               }
          }
       }  
}

</script>
<div id=’wmpPlayer’></div>
<input type=”button” name=”play” onclick=”player(’play’,'player’)” value=”Play”>
<input type=”button” name=”pause” onclick=”player(’pause’,'player’);” value=”Pause”>
<input type=”button” name=”stop” onclick=”player(’stop’,'player’)” value=”stop”>
</body>


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.