Tag Archive for jquery

jQuery Simple Style Switcher

/** @id sipleSwitcher
* @classDescription Very simple style switcher using jQuery
*/
$(document).ready(function(){
$('#styleSwitch .button').bind('click', function(){
$('body').removeClass();//remove all the other classes
$('#styleSwitch .button').removeClass('selected');
$(this).addClass('selected');
switch(this.id){
case 'style1':
$('body').addClass('style1');
break;
case 'style2':
$('body').addClass('style2');
break;
case 'style3':
$('body').addClass('style3');
break;
}
return false;
});
});

source

Error Messages for K2

Find & Replace this:
jQuery('#commenterror').show().html(request.responseText);

With:
jQuery('#commenterror').fadeIn().html(request.responseText).animate({opacity: 1.0}, 3000).fadeOut('slow');

source

Check for enter key using jQuery

$('#input_text').keyup(function(e) {
//alert(e.keyCode);
if(e.keyCode == 13) {
alert('Enter key was pressed.');
}
});

source

element’s absolute position in DOM Tree

myId = function(me){ return me.id ? '#' + me.id : '' }
myTag = function(me){ return me.tagName ? me.tagName.toLowerCase() : '' }
myClass = function(me){ return me.className ? '.' + me.className.split(' ').join('.') : '' }

breadcrumbs = function(me){
var path = [myTag(me) + myId(me) + myClass(me)];
$(me).parents().each(function() {
path[path.length] = myTag(this) + myId(this) + myClass(this);
});
return path.join(' < ');
}

$('body').click( function(){
alert( breadcrumbs(this) );
});

source

Checkbox status using jquery

var var_name = $('#checkbox_id').attr('checked')?1:0;

source

Radio button values using jquery

// for older jquery
var var_name = $("input[@name='radio_name']:checked").val();

// for jquery 1.3
var var_name = $("input[name='radio_name']:checked").val();

source

THE document ready

$(document).ready(function() {
// put all your jQuery goodness in here.
});

source

jquery collapse expand

INSIDE THE HEAD
......................................................................

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function()
{

$("#trigger").click(function(event) {
event.preventDefault();
$("#box").slideToggle();
});

$("#box a").click(function(event) {
event.preventDefault();
$("#box").slideUp();
});
});
</script>

HTML
......................................................................

<a href="#" id="trigger">TRIGGER</a>

<div id="box">
<a href="#" class="close">[x]</a>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren</p>
</div>

CSS
......................................................................
#box {
display: none;
}

source

jquery collapsible sidebar menu

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us">
<head>
<title>DL Demo</title>
<script src="http://jquery.com/src/jquery.js"></script>
<script>
$(document).ready(function(){
$("dd:not(:first)").hide();
$("dt a").click(function(){
$("dd:visible").slideUp("slow");
$(this).parent().next().slideDown("slow");
return false;
});
});
</script>
<style>
body { font-family: Arial; font-size: 16px; }
dl { width: 300px; }
dl,dd { margin: 0; }
dt { background: #F39; font-size: 18px; padding: 5px; margin: 2px; }
dt a { color: #FFF; }
dd a { color: #000; }
ul { list-style: none; padding: 5px; }
</style>
</head>

<body>
<dl>
<dt><a href="/">jQuery</a></dt>
<dd>
<ul>
<li><a href="/src/">Download</a></li>
<li><a href="/docs/">Documentation</a></li>
<li><a href="/blog/">Blog</a></li>

</ul>
</dd>
<dt><a href="/discuss/">Community</a></dt>
<dd>
<ul>
<li><a href="/discuss/">Mailing List</a></li>
<li><a href="/tutorials/">Tutorials</a></li>

<li><a href="/demos/">Demos</a></li>
<li><a href="/plugins/">Plugins</a></li>
</ul>
</dd>
<dt><a href="/dev/">Development</a></dt>
<dd>
<ul>

<li><a href="/src/">Source Code</a></li>
<li><a href="/dev/bugs/">Bug Tracking</a></li>
<li><a href="/dev/recent/">Recent Changes</a></li>
</ul>
</dd>
</dl>
</body>
</html>

source

Replace DOM element and keep classes and id

jQuery.fn.replaceWith = function(replacement) {
return this.each(function(){
element = $(this);
$(this)
.after(replacement).next()
.attr('class', element.attr('class')).attr('id',element.attr('id'))
.html(element.html())
.prev().remove();
});
};

/*
usage example

$('a#fooid').replaceWith('<span></span>');

before:
<a id="fooid" class="whatever">some text</a>

after:
<span id="fooid" class="whatever">some text</span>

*/

source