0

I'm not understanding function scope here. I have a button when clicked shows a dialog with a textarea. Inside that textarea I populate it with a url that someone can then copy for their camera setup.

<button id="axis-details" onclick="apikey('<?php echo $result_cameras[$i]["camera_hash"]; ?>');">API Key</button>

function apikey(camerahash)
{
   var $key = "http://myhost.com/notify.php/" +camerahash;
   return $key;
}

$(document).ready(function() {
   var $dialog = $('<div></div>');
$dialog.append('Please copy this key for camera setup: ')
       .append('<p><textarea id=\"textbox\">'+apikey(camerahash)+'</textarea></p>') //ERROR here that camerahash is not defined
       .append('<p>For more information see: <a href=\"http://www.myhost.com/forum/2-quickstart-docs\">setup</a></p>');
$dialog.dialog({
    autoOpen: false,
    title: 'API Key'
});

$('#axis-details').click(function(e) {
    e.preventDefault();
    $dialog.dialog('open');
});
});

Function apikey(camerahash) does return the value I expect. I get an error indicated above that camerahash is not defined. What am I doing wrong?

2
  • In your code you never initialize camerahash did you forget something? Commented Mar 8, 2012 at 16:20
  • the error indicating that camerahash is not defined is because camerahash is not defined... not sure how much clearer that can get Commented Mar 8, 2012 at 16:25

2 Answers 2

2

I think this is what you actually want:

<button id="axis-details">API Key</button>

function apikey(camerahash)
{
   var $key = "http://myhost.com/notify.php/" +camerahash;
   return $key;
}

$(document).ready(function() {
   var $dialog = $('<div></div>');
$dialog.append('Please copy this key for camera setup: ')
       .append('<p><textarea id=\"textbox\">'+apikey(<?php echo $result_cameras[$i]["camera_hash"]; ?>)+'</textarea></p>') //ERROR here that camerahash is not defined
       .append('<p>For more information see: <a href=\"http://www.myhost.com/forum/2-quickstart-docs\">setup</a></p>');
$dialog.dialog({
    autoOpen: false,
    title: 'API Key'
});

$('#axis-details').click(function(e) {
    e.preventDefault();
    $dialog.dialog('open');
});
});
Sign up to request clarification or add additional context in comments.

1 Comment

I wish it was that simple. The php code goes through a loop to print out the camera_hash. Thanks for the help, I guess I've got a bigger problem.
2

It is only defined in your apikey function, you need to also pass it in from your jquery method,

 .append('<p><textarea id=\"textbox\">'+apikey('<?php echo $result_cameras[$i]["camera_hash"]; ?>')+'</textarea></p>')

Or even easier, modify your function to not need the input,

function apikey()
{
   var $key = "http://myhost.com/notify.php/" +'<?php echo $result_cameras[$i]["camera_hash"]; ?>';
   return $key;
}

1 Comment

Or even easier still, don't use a function!

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.