0

I'm using this function:

    $(function echo_content(name, content) {
        var random1 = Math.ceil(Math.random()*500);
        var random2 = Math.ceil(Math.random()*1000);
        alert(content);
        $('#TheDiv').show("slow");
});

And i'm calling this function on my webpage like this:

    <td>Content <script>echo_content("MyName", "The Content"); </script></td>

The problem is, that the function alerts undefined instead of The Content. What am I doing wrong?

1 Answer 1

4

This is not a JQuery DOM ready handler. Just define your function like that:

function echo_content(name, content) {
    var random1 = Math.ceil(Math.random() * 500);
    var random2 = Math.ceil(Math.random() * 1000);
    alert(content);
    $("#TheDiv").show("slow");
}

BTW, #TheDiv block should be loaded before you call the function.

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

4 Comments

Your suggestion does not work. It doesn't show the alert. Am I doing something wrong?
Can you please tell me why this doesn't work: jsfiddle.net/DSgeb/2 ? My echo_content function is in scripts.js which is included into website. Thanks! :)
@JernejPangeršič This doesn't work because your echo_content function is not defined in the scope. JSFiddle places the JS block to DOM ready handler. Use my example to get it work in JSFiddle. Check your scripts.js is included before you call the function.
How silly of me, I had scripts.js included afterwards :) Thank you very much! :)

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.