1

I try to do simple if/else statements, so for example "If value on slider is 100, append p tag with some text to miles class". I know it is something simple, but I look through the jQuery docs and didn't find the solution for me.

HTML:

<html><head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
    <div class="wrapper">
        <div class="miles"></div>
        <div class="battery-slider">
            <input class="slider-range" type="range" value="0" min="75" max="250" step="25">
            <span class="slider-value">0</span>
        </div>
    </div>
</body>
</html>

JavaScript:

var rangeSlider = function(){
    var slider = $('.battery-slider'),
    range = $('.slider-range'),
    value = $('.slider-value');
    miles = $('.miles');

    slider.each(function(){
        value.each(function(){
            var value = $(this).prev().attr('value');
            $(this).html(value);
        });
        range.on('input', function(){
            $(this).next(value).html(this.value);
        });
    });

    if ( $(range).val(value) == 100 ) {
        $(miles).append( "<p>Pass</p>" );
    }
    else {
        $(miles).append( "<p>Fail</p>" );
    }
};
rangeSlider();

1 Answer 1

2

Your requirements are a bit unclear, but perhaps you're looking for something like this:

var rangeSlider = function(){
    var slider = $('.battery-slider'),
    range = $('.slider-range'),
    value = $('.slider-value');
    miles = $('.miles');

    slider.each(function(){
      value.each(function(){
          var value = $(this).prev().attr('value');
          $(this).html(value);
      });
      range.on('input', function(){
        if ( this.value == 100 ) {
            $(miles).append( "<p>Pass</p>" );
        }
        else {
            $(miles).append( "<p>Fail</p>" );
        }

        $(this).next(value).html(this.value);
      });
    });
};
rangeSlider();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
    <div class="battery-slider">
        <input class="slider-range" type="range" value="0" min="75" max="250" step="25">
        <span class="slider-value">0</span>
    </div>
    <div class="miles"></div>
</div>

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

4 Comments

I'm looking for a little bit different thing, I need it to print "Pass" when value on the slider = 100, if it 75 or others print "Fail"
Is that not what it's already doing? I did edit the snipped just now so as to remove your original non-functioning ifstatement, but that won't alter the sinppet's functionality.
Oops, I'm sorry....I didn't look through your code snippet properly. Thanks a lot, now one more thing. How to make it appear only once if pass or fail?
Change the append calls to html calls, e.g. $(miles).html( "<p>Pass</p>" );

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.