0

I want to add a toggle button dynamically. The toggle button will look like in Flip toggle switch Example. I tried like that but i get a different output like data-role="slider" is not working. the code

$('button').click( function() {
   var element  =  $ ('#test');
    var toggle_button = '<select data-role="slider"><option value="off">Off</option><option value="on">On</option></select>';
    element.append(toggle_button);
});

My JsFiddle is : http://jsfiddle.net/4Fk9L/1/

What I do wrong here?

2
  • 1
    I think you'll have to call the $('select').slider(); because you are adding it after the DOM is ready Commented Dec 3, 2012 at 10:19
  • Yep.. thats the solution. Thanks.. Commented Dec 3, 2012 at 10:21

1 Answer 1

1

call the switch function after the element is appended..

 $('button').click( function() {
    var element  =  $ ('#test');
    var toggle_button = '<select data-role="slider"><option value="off">Off</option>   <option value="on">On</option></select>';
    element.append(toggle_button);
    $('select').slider();  //call plugin function here
});

here is the fiddle

http://jsfiddle.net/4Fk9L/2/

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

1 Comment

I was first ;) jsfiddle.net/4Fk9L/1 haha (note the /1) lol i have no idea why im telling xD

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.