0

EDIT: the 'more' link is built from here:

document.getElementById("elenco_fiveboard").insertAdjacentHTML('beforeend', 
    "<li>" +
    "Titolo: " + valore_comando + " " + 
    "(<a href='javascript:getInfo("+ valore_comando +");'>" +
    "more" + "</a>)" +
    "</li>"); 

So I have to wrap valore_comando with hyphens but I get error trying to write

"(<a href='javascript:getInfo(""+ valore_comando +"");'>" +

or

"(<a href='javascript:getInfo('"+ valore_comando +"');'>" +

Sorry but I am not so strong with JS syntax and I am starting from some code that is not my own.

I have this simple JS function:

  getInfo = function(title){ 
    worker.port.postMessage("maggiori_informazioni:" + title);
  }

I run it passing to the variable title a value but I always get an error. If title is 'example' then JS try to run

getInfo = function(example)

and I get the error:

Reference Error: example is not defined. If title has more than one word: 'first example' then JS will try to run

getInfo = function(first example)

and I get the error:

Syntax error: missing ) after argument list (looking for parentesys after 'first').

What am I missing here?? This code is part of my first test with webSockets. If you want to see the full code you can open this index page and put a value on the first alert you see. That value is the title that you will see on the dashboard page. The error can be reproduced trying to hit 'More' after each defined title in dashboard. Sorry for italian in the site but it's a user requirement.

2
  • 4
    getInfo("this is a title"); Commented Aug 11, 2015 at 17:00
  • What am I missing here?? -- quotation marks around your literal strings. Commented Aug 11, 2015 at 17:03

4 Answers 4

2

getInfo having reference to your function. So you can call this by:

getInfo(value);

Use quotes if you are passing string value and if you are passing integer value you can simply pass.

For instance:

getInfo("Message Title"); //string
getInfo(3); // Integer 

DEMO


You can also call like this:

getInfo = function(title){ 
    alert("maggiori_informazioni:" + title);
  }("String");

DEMO

Updates:

You can use this:

"(<a href='"+getInfo(valore_comando)+";'>" +
Sign up to request clarification or add additional context in comments.

Comments

1
  1. the function has been assigned to getInfo so please call getInfo()
  2. string should be enclosed with ''

So the code should be

getInfo('example')

1 Comment

This answer gets to the actual point. getInfo is assigned to a function. So call it, don't reassign it.
0

You're going to have to escape some quotation marks.

"(<a href=\"javascript:getInfo('"+ valore_comando +"');\">" +

The \ character before the quotation marks for the href attribute tells the JavaScript parser that they're part of the string instead of the beginning or end of a string.

1 Comment

Thanks for the hint. It was the missing combination I haven't tried.
0

The problem is that example is not defined. If you wanted to call the function with example as input it needs either single quotes or double quotes around it. "example" or 'example'.

getInfo("example"); // Works as intended

Another option is to create a variable containing the string, and pass that to the function.

var myVariable = "example";
getInfo(myVariable); // Still works, myVariable is a string with value "example"

Comments