Change HTML data attribute with Jquery

Asked

Viewed 557 times

2

I have an input that would like to change the date-min and data-max with the value brought by AJAX by changing the value of select category_id. Ajax is bringing the value correctly, but does not change the value of data-min and data-max.

HTML

<select id="category_id" class="form-control" name="category_id" required>
    <option value="">Selecione</option>
    @foreach($categories as $category)
        <option value="{{$category->id}}">{{ $category->name }}</option>
    @endforeach
</select>
<input type="text" id="range_01" data-min="0" data-max="0">

$("#category_id").change(function() {
    var category = $(this).val();
    $.ajax({
        type:'GET',
        url:'/customer/orders/lowerValueService/' + category,
        success:function(data){
            var value = data;
            console.log(value);
            $("#range_01").data("min", value);
        },
        error: function(){
            console.log("Erro");
        }
    });

    $.ajax({
        type:'GET',
        url:'/customer/orders/highestValueService/' + category,
        success:function(data){
            var value = data;
            console.log(value);
            $("#range_01").data("max", value);
        },
        error: function(){
            console.log("Erro");
        }
    });
});
  • Forehead with $("#range_01").attr("data-max", value);, works?

  • Didn’t work..

  • I had done something wrong, it did work.

  • The .data() jQuery works in a special way... where you need to use this data-xxx in the code after you give it the value of ajax?

  • It worked the way it went. You can put as answer.

  • Yes, I’ll give you an answer, but I’m curious where you use that value, because it may be necessary to use it elsewhere, and I’d like to explain (to others who read the answer) why .data not work.

  • Needed when changing select to change the date-min and date-max to use in another function.

  • Ok, show this other function and the function that select runs when it changes as well.

  • This is the function of a plugin. http://ionden.com/a/plugins/ion.rangeSlider/en.html

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.