73

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?

14
  • Are you trying to use HTML5 data attributes? Commented Aug 3, 2012 at 10:43
  • Seems to work: jsfiddle.net/Gm9D2 Commented Aug 3, 2012 at 10:43
  • What does "not working" mean exactly? Commented Aug 3, 2012 at 10:44
  • actually it a part of my code . i think i should put up my whole code it not working in tht Commented Aug 3, 2012 at 10:44
  • 1
    post an online example of your full code, in jsfiddle or similar Commented Aug 3, 2012 at 10:53

2 Answers 2

61

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/

Sign up to request clarification or add additional context in comments.

1 Comment

thanks i would have needed this too in writing my code further hehehehe :)
50

It is working you have to check attr after assigning value

LiveDemo

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

alert($('#amount').attr( 'datamin'));​

3 Comments

What about to set multiple attr in one call?
$( "#greatphoto" ).attr({ alt: "Beijing Brush Seller", title: "photo by Kelly Clark" }); check details here, api.jquery.com/attr
@Adil do you have any idea for this stackoverflow.com/questions/34311726/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.