0

I have a problem, I have a js code which append a span element. My problem is it keeps disappearing, its not a bug or something.

PS: I'm using jquery ui slider.What I'm doing is display the current value of my slider using the appended element(span). The only problem is it keeps disappearing when I click my other slider.

Here is all my code: HTML

<div id = "opacity" class="slider-range"></div><br>
<div id = "volume" class="slider-range"></div><br>

Javascript

var append_element = $('<span></span>');
        $('.slider-range').slider( {
            animate : true,
        });

        $("#opacity").slider({
            min     : 0,
            max     : 100,
            value   : 60,
            step    : 10,
            change  : function(event, ui) {
                slider_value('#opacity', ui.value);
            }
        });

        $("#volume").slider({
            min     : 0,
            max     : 100,
            value   : 20,
            step    : 10,
            change  : function(event, ui) {
                slider_value('#volume', ui.value);
            }
        });

        function slider_value(id, response) {
            append_element.removeClass().appendTo(id);
            //remove '#' / get id
            id = $(id).attr('id');

            var new_span = $('span').addClass('slider-value-'+id);
            $(new_span).html(response);
            console.log(new_span);
        }

Anyone. Pulling my hair here T_T.

Thanks.

Wondering Coder
  • 1,652
  • 9
  • 31
  • 51

1 Answers1

1

You are just using one span at the whole page.. I think if you won't letting disappear it you have to use more than just one!

So you can use this code:

var append_element = '<span></span>';
    $('.slider-range').slider( {
        animate : true,
    });

    $("#opacity").slider({
        min     : 0,
        max     : 100,
        value   : 60,
        step    : 10,
        change  : function(event, ui) {
            slider_value('#opacity', ui.value);
        }
    });

    $("#volume").slider({
        min     : 0,
        max     : 100,
        value   : 20,
        step    : 10,
        change  : function(event, ui) {
            slider_value('#volume', ui.value);
        }
    });

    function slider_value(id, response) {
        $(id).html(append_element);
        //remove '#' / get id
        id = $(id).attr('id');

        var new_span = $('#'+id+'>span').addClass('slider-value-'+id);
        $(new_span).html(response);
        console.log(new_span);
    }

​ Example: http://jsfiddle.net/c7myp/

movabo
  • 411
  • 2
  • 10