0

I am using the code bellow to use Jquery UI slider.

<script>
$(function() {
$( "#slider" ).slider({
value:100,
min: 0,
max: 500,
step: 50,
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.value );
}
});
$( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
});
</script>

How is this possible to ad this script dynamicly inside a div by pressing a button?

<div id="addscript"></div>

I want to do this that way coz i am generating the slider and some text boxes dynamic like this way.

Thanks

2
  • Are you saying you want to execute the script when you press the button? If so you want to use the click event of the button and a function Commented Jun 8, 2014 at 0:35
  • Why don't you just put it in a function and call it on click ? Commented Jun 8, 2014 at 0:35

1 Answer 1

1

As hinted at in my comment, you will want something like this, ensure you swap out 'button-id' for whatever ID is assigned to yours.

<script>
    $(function() {
        $('#button-id').on('click', function() {
            attachSlider();
        });
        function attachSlider() {
            $( "#slider" ).slider({
                value:100,
                min: 0,
                max: 500,
                step: 50,
                slide: function( event, ui ) {
                    $( "#amount" ).val( "$" + ui.value );
                }
            });
            $( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) );
        }
    });
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

No i already have a button and when i am pressing it i am adding a slider inside a div. next to each slider i have a div and i want to display a number when i am sliding the slider.. but i to work each slider seperately.
You have accepted my answer but I get the impression from this comment that you would like further assistance? Do you want me to create a jsfiddle for what you have described?

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.