Tuesday, March 13, 2012

How to find and replace hidden field value & attributes value in jquery


<input id="UserIds" type="hidden" value="1642,6172,2485,2281,
data-userids="1642,6172,2485,2281," name="UserIds">



 <script type="text/javascript">
$(document).ready(function(){
     $('#UserIds').replaceVal('2485,', '');


     $('#UserIds').replaceAttr('2485,', '','data-userids');
});


(function ($) {

    $.fn.replaceAttr = function (match, replaceWith, attrObj) {
        return $(this).attr(attrObj, $(this).attr('value').replace(match, replaceWith));
    };


    $.fn.replaceVal = function (match, replaceWith) {
        return $(this).replaceAttr(match, replaceWith, 'value');
    };
                            
                    //       OR ( above or below one only use it.)



    $.fn.replaceVal = function (match, replaceWith) {
         return $(this).attr('value', $(this).attr('value').replace(match, replaceWith));
    };



})(jQuery);

 </script>



Output:
<input id="UserIds" type="hidden" value="1642,6172,2281,"
  data-userids ="1642,6172,2281, name="UserIds">

No comments:

Post a Comment