最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change HTML range slider value with button? - Stack Overflow

programmeradmin6浏览0评论

I am using a HTML range slider to zoom in and zoom out an image. I also want to show two buttons with slider like + for zoom and − for zoom out. When a user clicks on either of these buttons, the slider should also be moved. But I am unable to do this. Here is my code.

<div id="slider">
    <input id="slide" type="range" min="330" max="1200" step="30" value="330" onchange="ImageViewer.sliderAction(this.value)" />
</div>

Here is my function:

var sliderAction = function (value) {
    $sliderChosen.html(value);
    $("#img").width(value);
    $("#img").height(value);
};

When I click the button, I tried this:

btnZoomIn.click(function() {
    $("#slide").value();  //Want to change value but value is undefined.
});

Is there any way to change slider value and move it as well on button click?

I am using a HTML range slider to zoom in and zoom out an image. I also want to show two buttons with slider like + for zoom and − for zoom out. When a user clicks on either of these buttons, the slider should also be moved. But I am unable to do this. Here is my code.

<div id="slider">
    <input id="slide" type="range" min="330" max="1200" step="30" value="330" onchange="ImageViewer.sliderAction(this.value)" />
</div>

Here is my function:

var sliderAction = function (value) {
    $sliderChosen.html(value);
    $("#img").width(value);
    $("#img").height(value);
};

When I click the button, I tried this:

btnZoomIn.click(function() {
    $("#slide").value();  //Want to change value but value is undefined.
});

Is there any way to change slider value and move it as well on button click?

Share Improve this question edited Dec 16, 2015 at 10:57 Milind Anantwar 82.2k25 gold badges96 silver badges127 bronze badges asked Dec 16, 2015 at 10:37 Umer WaheedUmer Waheed 4,5847 gold badges45 silver badges68 bronze badges 2
  • .val() value is null – Umer Waheed Commented Dec 16, 2015 at 10:46
  • how about attr('value')? – madalinivascu Commented Dec 16, 2015 at 10:49
Add a ment  | 

2 Answers 2

Reset to default 11

use val()

 btnZoomIn.click(function() {
               //if value < max
               $("#slide").val(parseInt($("#slide").val())+30);  
               $("#slide").trigger('change');
            });

https://jsfiddle/9jfst447/

You can use callback function of .val() and then trigger onchange event:

var sliderelement = $( "#slide" );
btnZoomIn.click(function() {
 sliderelement.val(function( index, value ) {
     return parseInt(value,10) + 30;
 });
 sliderelement[0].onchange()
});
发布评论

评论列表(0)

  1. 暂无评论