All posts tagged jQuery

jQuery: How to position one element relative to another?

I developed a social networking control for asp.net

http://www.smartwebsource.info/smartshare/Default.aspx

where i need to show the div relative to the parent control.

I have a div i want to show another div when click it.

There i use this code to show the div right side of my image.

  $(document).ready(function() {
        $('a#share').click(function(e) {
            e.preventDefault();
            var pos = $("#share").offset();
            var width = $("#share").width();
             $("#share-box").css( { "left": (pos.left + width) + "px", "top":pos.top + "px" } );
           $('div#share-box').show();
           $('div#share-box a.close-share').click(function(e) {
                e.preventDefault();
                $('div#share-box').hide();
            });
        });
    });
 

Here is the CSS

div#share-box {
/*    margin-left: 300px;*/
    position: absolute;
}
div.share-inner-box {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 9px solid #CDCDCD;
    padding-bottom: 10px;
    position: absolute;
    width: 400px;
    z-index: 999;
}

write another way , this is just an example not related to the above code

var showMenu = function(el, menu) {
        //get the position of the placeholder element
        var pos = $(el).offset();
        var eWidth = $(el).outerWidth();
        var mWidth = $(menu).outerWidth();
        var left = (pos.left + eWidth - mWidth) + "px";
        var top = 3+pos.top + "px";
        //show the menu directly over the placeholder
        $(menu).css( {
                position: 'absolute',
                zIndex: 5000,
                left: left,
                top: top
        } );
       $(menu).hide().fadeIn();
};

Hope this will help you.

How do you get the current value from a select list

Today i am developing a client management system where i fall a problem, i am not able to get the current select list values. I search on google but visiting many site i am not get good solution. At last i got the solution in one blog . So that i think to share this problem solution with you. This problem is search by many people in google by “How do you get the current value from a select list?”

To get the current value is very simple using val().

$('#selectList').val();

View Demo:

$('#selectList :selected').text()

I will create the demo and code below. This would be how to set a select multiple to an array called, “foo”.

      var foo = [];

      $('#multiple :selected').each(function(i, selected){

          foo[i] = $(selected).text();

      });

Facebook like Expanding Textbox with Jquery.

Today i will show you how to implement Facebook like expanding textbox with Jquery.Thanks to d Josh Bush for his nice watermarkinput java script file and Srinivas Tamada to developed this script.
Facebook like Expanding Textbox with Jquery.
Javascript Code
It works with Jquery latest version 1.4.2. $(‘#content’).focus(function(){} – content is the ID of the textbox. Using $(this).animate() expanding textbox height and using $(“#button_block”).slideDown() showing the update button

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function()
{

$("#content").focus(function()
{
$(this).animate({"height": "85px",}, "fast" );
$("#button_block").slideDown("fast");
return false;
});

$("#cancel").click(function()
{
$("#content").animate({"height": "30px",}, "fast" );
$("#button_block").slideUp("fast");
return false;
});

});
</script>




//HTML Code

<form method="post" action="">
<textarea  id="content"></textarea>
<div id="button_block">
<input type="submit" id="button" value=" Share "/>
<input type="submit" id='cancel' value=" cancel" />
</div>
</form>

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

body
{
font-family:Arial, Helvetica, sans-serif;
}
#content
{
width:450px; height:30px;
border:solid 2px #006699;
font-family:Arial, Helvetica, sans-serif;
font-size:14px;

}
#button
{
background-color:#3B5998;
color:#ffffff;
font-size:13px;
font-weight:bold;
padding:4px;
}
#cancel
{
background-color:#3B5998;
color:#ffffff;
font-size:13px;

padding:4px;
margin-left:10px;
}
#content
{
font-size:14px;

}
#button_block
{
display:none;


}

Pagination with jQuery, MySQL and PHP

From many days i am thinking to make a pagination script using php and jquery. Today i am making a pagination script for you. How to implement Pagination with jQuery, PHP and MySQL. It is a simple tutorial. It’s looks big but very simple script. Take a look at this screen shot.
Pagination with jQuery, MySQL and PHP.

The tutorial contains three PHP files and two js files and one css file includes jQuery plugin.

-config.php (Database Configuration)
-pagination.php
-pagination_data.php
-jquery.js
-jquery_pagination.js
–style.css

We also include the database script in the download zip file with data so that you can run this code and check.

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-asp.net.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

Database Table

CREATE TABLE IF NOT EXISTS `northwind_products` (
  `ProductID` int(1) NOT NULL AUTO_INCREMENT,
  `ProductName` varchar(256) NOT NULL,
  `UnitPrice` decimal(4,2) NOT NULL,
  `UnitsInStock` int(10) NOT NULL,
  PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=78 ;

jquery_pagination.js
Contains javascript this script works like a data controller.

$(document).ready(function()
{
//Data Loading Image
function Data_Load()
{
$("#loading").fadeIn(900,0);
$("#loading").html('<img src="bigLoader.gif" />');
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};

//Default Starting Page Results
$("#pagination li:first")
.css({'color' : '#FF0084'}).css({'border' : 'none'});
Data_Load();
$("#content").load("pagination_data.php?page=1", Hide_Load());

//Pagination Click
$("#pagination li").click(function(){
Data_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});

$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});

//Loading Data
var pageNum = this.id;
$("#content").load("pagination_data.php?page=" + pageNum, Hide_Load());
});

});

config.php
You have to change hostname, username, password and databasename.

<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "learneveryday_tutorial";
$db = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die('Error connection to database server: ' . mysql_error());
mysql_select_db($mysql_database, $db)
or die('Error connection to database server: ' . mysql_error());
?>

pagination.php
User interface page.

<?php
include('config.php');
$per_page = 10;

//Calculating no of pages
$sql = "select count(*) from northwind_products";
$result = mysql_query($sql);
$count = mysql_fetch_row($result);
$pages = ceil($count[0]/$per_page);

?>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery_pagination.js"></script>
<link rel="stylesheet" href="style.css" type="text/css" />

<div id="loading" ></div>
<div id="content" ></div>
<ul id="pagination">
<?php
//Pagination Numbers
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>

pagination_data.php
Simple php script display data from the messages table.

<?php
include('config.php');
$per_page = 10;
if($_GET)
{
$page=$_GET['page'];
}

$start = ($page-1)*$per_page;
$sql = "select * from northwind_products order by ProductID limit $start,$per_page";
$result = mysql_query($sql);
?>
<table width="800px">
<?php
while($row = mysql_fetch_array($result))
{
$ProductID=$row['ProductID'];
$ProductName=$row['ProductName'];
?>
<tr>
<td><?php echo $ProductID; ?></td>
<td><?php echo $ProductName; ?></td>
</tr>
<?php
}
?>
</table>

CSS Code
CSS code for page numbers.

#loading
{
width: 100%;
position: absolute;
}
li
{
list-style: none;
float: left;
margin-right: 16px;
padding:5px;
border:solid 1px #dddddd;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}

You can download the full code from bellow link.

Animated AJAX Record Deletion Using jQuery

In WordPress you see that when method of individual article deletion there is an animation. You click the delete link, the menu item animates red, and the item disappears. Here’s how to achieve that functionality with jQuery JavaScript.

The PHP – Content & Header

The following snippet goes at the top of the page:

if(isset($_GET['delete']))
{
	$query = 'DELETE FROM my_table WHERE item_id = '.(int)$_GET['delete'];
	$result = mysql_query($query,$link);
}

The following is used to display the records:

$query = 'SELECT * FROM my_table ORDER BY title ASC';
$result = mysql_query($query,$link);
while($row = mysql_fetch_assoc($result))
{
	echo '<div class="record" id="record-',$row['item_id'],'">
				<a href="?delete=%27,$row[%27item_id%27],%27" class="delete">Delete</a>
				<strong>',$row['title'],'</strong>
			</div>';
}

The jQuery JavaScript

$(document).ready(function() {
	$('a.delete').click(function(e) {
		e.preventDefault();
		var parent = $(this).parent();
		$.ajax({
			type: 'get',
			url: 'jquery-record-delete.php',
			data: 'ajax=1&delete=' + parent.attr('id').replace('record-',''),
			beforeSend: function() {
				parent.animate({'backgroundColor':'#fb6c6c'},300);
			},
			success: function() {
				parent.slideUp(300,function() {
					parent.remove();
				});
			}
		});
	});
});

For every link, we add a click event that triggers the AJAX request. During the request, we transition the containing element to a red background. When the AJAX request returns a “success” response, we slide the element off of the screen.

Thanks David Walsh for his article.
You can find the original article http://davidwalsh.name/animated-ajax-jquery
In future will are coming some jquery and php tutorial.