Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting new value for an attribute using jQuery

I am trying to set a new vale of a custom attribute of a div using attr(). I found out it can be done using .attr( attributeName, value ), but when I try it it's not working.

Here is the part of my code I am interested in:

$('#amount').attr( 'datamin','1000') 

and the div that has the custom attribute is

<div id="amount" datamin=""></div> 


Here is the whole example:

$(function() {     $( "#slider-range" ).slider({         range: true,         min: <?php echo $min_val;?>,         max: <?php echo $max_val;?>,         values: [ <?php echo $min_val;?>, <?php echo $max_val;?> ],         slide: function( event, ui ) {             $( "#amount" ).html( "<?php echo $currency_chk_i;?>" + ui.values[ 0 ] + " - <?php echo $currency_chk_i;?>" + ui.values[ 1 ] );              $('#amount').attr( 'datamin','1000');          },         change: function(event, ui) {             // when the user change the slider         },         stop: function(event, ui) {         alert(ui.values[0]);             // when the user stopped changing the slider          }     });     $( "#amount" ).html( "<?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 0 ) +         " - <?php echo $currency_chk_i;?>" + $( "#slider-range" ).slider( "values", 1 ) );  }); 


Can anyone point out where I am wrong or any other way out?

like image 615
Sakshi Sharma Avatar asked Aug 03 '12 10:08

Sakshi Sharma


People also ask

How set value to data attribute in jQuery?

To set an attribute and value by using a function using this below syntax. $(selector). attr(attribute,function(index,currentvalue)) ; To set multiple attributes and values using this below syntax.

How do you set an attribute and its value?

Element.setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .

Which function is used to set an attribute in jQuery?

Set Attributes - attr() The jQuery attr() method is also used to set/change attribute values.

What does VAL () do in jQuery?

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined . When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .


1 Answers

Works fine for me

See example here. http://jsfiddle.net/blowsie/c6VAy/

Make sure your jquery is inside $(document).ready function or similar.

Also you can improve your code by using jquery data

$('#amount').data('min','1000');  <div id="amount" data-min=""></div> 

Update,

A working example of your full code (pretty much) here. http://jsfiddle.net/blowsie/c6VAy/3/

like image 194
Blowsie Avatar answered Sep 22 '22 21:09

Blowsie