Jquery: How to select more than one element

May 11, 2011

if you have two elements like:

<input id=”name” />

and

<span id=”address”>Berlin</span>

then using jquery you can select them like:

$(‘#name, #address’)

or

$(‘#name’).add(‘#address’)


Jquery: How to work with same class name elements event

May 11, 2011

Assumed that you have some links with same class. like:

<a class=”delete_user” href=”#”>delete</a>
<a class=”delete_user” href=”#”>delete</a>
<a class=”delete_user” href=”#”>delete</a>
.
.
.
<a class=”delete_user” href=”#”>delete</a>

on click event you want to delete the user info from database table. then just add a new attribute “user_id” here “user_id: is the primary key of “user” table. like:

<a user_id=”1″ class=”delete_user” href=”#”>delete</a>
<a user_id=”2″ class=”delete_user” href=”#”>delete</a>
<a user_id=”3″ class=”delete_user” href=”#”>delete</a>
.
.
.
<a user_id=”10″ class=”delete_user” href=”#”>delete</a>

then the Jquery code is:

(function(){

//DELETE USER
$(“.delete_user”).click(function(){
var user_id = $(this).attr(‘user_id’);
//here you delete operation using user_id
});

})();


Clear Textfield Default Value onClick using JavaScript

January 13, 2010

Sometime we have text fields with default value like “Please enter you first name”, “Please enter you phone number” etc. We want to clear this default text when user click on the field and if user do not give any input the it will put the default value again.

Javascript code:

function clearValue ( field ) {
if ( field.defaultValue == field.value ) field.value=;
else if ( field.value == ) field.value= field.defaultValue;
}

Use:

<input type=”text” name=”mobile” value=”Phone/Mobile Number” onFocus=”clearValue(this)” onBlur=”clearValue(this)”/>

Download Source Code: click here. (Unzip this zipped file, open clearText.html file with any browser then click on the text field.)


Follow

Get every new post delivered to your Inbox.