Reputation: 5417
I have a slider functioning here.
What i want to do is when slider goes in between 10-20, 21-40, 41-60, 61-80, 81-100 i want image to be replaced with another, you can see a default image which i want to be replaced...
So for example if slider is in between 21-40 image source should be changed to
<img src="images/slide_2.jpg" width="459" height="315">
and so on.. how to do it ?
Upvotes: 2
Views: 7811
Reputation: 85378
Live Example:
JS:
$("#slider").change(function() {
sVal = $(this).val();
if(sVal > 21 && sVal <= 40) {
$('#theImage').attr('src', 'http://jquerymobile.com/demos/1.0b2/docs/toolbars/glyphish-icons/21-skull.png');
}
if(sVal > 41 && sVal <= 60) {
$('#theImage').attr('src', 'http://jquerymobile.com/demos/1.0b2/docs/toolbars/glyphish-icons/18-envelope.png');
}
if(sVal > 61 && sVal <= 80) {
$('#theImage').attr('src', 'http://jquerymobile.com/demos/1.0b2/docs/toolbars/glyphish-icons/100-coffee.png');
}
if(sVal > 81 && sVal <= 100) {
$('#theImage').attr('src', 'http://jquerymobile.com/demos/1.0b2/docs/toolbars/glyphish-icons/88-beermug.png');
}
});
HTML:
<div data-role="page" id="slider-test" data-theme="a">
<div data-role="content">
<div data-role="fieldcontain">
<label for="slider">Input slider:
<img id="theImage" src="http://jquerymobile.com/demos/1.0b2/docs/toolbars/glyphish-icons/09-chat2.png" alt="slider images" />
</label>
<input type="range" name="slider" id="slider" value="0" min="0" max="100" />
</div>
</div>
</div>
Upvotes: 3
Reputation: 10147
Easy, if you add type attribute to your slider tag like so type="slider"
, it'll become a html5 slider object and will have a change event where you can check its current value. So your code would like something like this:
$(document).ready(function () {
$("#slider").change(function () {
if (slider.value > 20 && slider.value < 41)
$("#imgMap").attr("src", "images/slide_2.jpg");
});
});
if you give your image id="imgMap"
<img id="imgMap" src="images/slide_1.jpg" width="459" height="315">
<input type="slider" name="slider" id="slider" value="47" min="0" max="100"
data-type="range">
Upvotes: 0