0

I have a script inside of my HTML file. For example

<div id='mydiv'>Hello World</div>

<script>
$(document).ready(function(){
   var newtext = 'Bye-bye world';
   $('#mydiv').html(newtext);
});
</script>

Now I'd like to move the script to external file and make so, that user can define which html to add to which div. For example

<div id='mydiv'>Hello World</div>
<script>
$(document).ready(function(){
   $('#mydiv').changetext({
      'newtext':'My new text'
   });
});
</script>

How can it be done? Thx

0

1 Answer 1

1

To use the syntax of your second example you would need to create a plugin.

(function( $ ){
    $.fn.changetext = function( options ) {  
        var settings = $.extend( {
            'newtext'         : 'Default text'
        }, options);

        $(this).text(settings.newtext);
    };
})( jQuery );

See this link for more information: http://docs.jquery.com/Plugins/Authoring.

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

Comments

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.