Thursday, May 2, 2013

Add Remove Attributes - jQuery

Add new id

$(element).attr('id', 'newID');

remove id

<img class="thumb" id="thumb" src="img/1_1.jpg" />

$('img#thumb').removeAttr('id');

Example :

<DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>

$(document).ready(function(){
  $("#save").click(function(){
    $("input").attr("readonly", true);
$("input#input").removeAttr('id');
$(".input").attr('id', 'aftersave');
 });
});

$(document).ready(function(){
  $("#btnsubmit").click(function(){
    $("input").attr("readonly", false);
$(".input").attr('id', 'input');
  });
});
</script>
<style type="text/css">
#input{
border:solid 2px #00F;
}
#aftersave{
border:solid 0px #000;
}
#save{
background:#CFC;
}
</style>
</head>
<body>
<p>Simple button function</p>

<input readonly type="text" class="input" name="Language" border="3px" value="English">
<br>
<input type="button" id="btnsubmit" value="edit">
<input type="button" id="save" value="save">
</body>
</html>

0 Comments:

Post a Comment