Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find Variable when called function from onClick

Tags:

javascript

I'm trying to call a function with an onClick command, but I get the error "Can't find variable" in the Safari console and no action is taken. I can't see any errors, but I must be missing something that is causing this to fail.

The link

<a href="JavaScript:void(0);" onclick="controlWallMonitor('layout','Playback');">Show playback layout</a>

The Javascript

<script type="text/javascript">
$(document).ready(function() {
    function controlWallMonitor(variable, option) {
        var WallMonitor = "10.0.50.163:9000";
        $.ajax({
            url: 'changelayout.php?target=' + WallMonitor + '&variable=' + variable + '&option=' + option,
        });
        return false;
    };
});

I'm sure it'll end up being incredibly obvious, but I have been googling and trying things for about 2 hours now and can't for the life of me see the problem.

Cheers.

like image 632
teknetia Avatar asked Nov 02 '12 01:11

teknetia


People also ask

Does Onclick only work once?

addEventListener can add multiple events to a particular element. onclick can add only a single event to an element.

Can we use Onclick in button tag?

The onclick attribute is an event attribute that is supported by all browsers. It appears when the user clicks on a button element. If you want to make a button onclick, you need to add the onclick event attribute to the <button> element.

Is Onclick a callback?

We can create an HTML button element and use the native onclick event. The value of this event will be a callback function activateLasers() . So when the button is clicked, activateLasers() is also called and adds an alert to our screen.

Is Onclick a DOM event?

There are a number of DOM (Document Object Model) events that you can listen for in JavaScript, but onclick and onload are among the most common.


1 Answers

Remove the $(document).ready call from around the function, all you have to do is put it after the jQuery script inclusion.

EDIT: Also, instead of using onclick, use a jQuery event handler:

HTML:

<a href="JavaScript:void(0);" class="playback">Show playback layout</a>

JS:

$(document).ready(function(){
    $("a.playback").click(function(){ controlWallMonitor('layout', 'Playback'); });
});
like image 167
PitaJ Avatar answered Oct 28 '22 03:10

PitaJ