views:

96

answers:

5

Hey, I'm busy on little JavaScript item, but I got confused by the next rule;

<a href="#" onclick="removeMap('+ \"test\" +')" style="float:right; margin-right: 30px;"><img src="images/icons/collapse.gif"></a><br/>'

It doesn't take the onclick remove map action. I don't know what I have to put between the () to make I can put text there. Does somebody have the solution?

Thanks

+7  A: 

Try just :

onclick="removeMap('test')"

if test is a variable:

onclick="removeMap(test)"

The \" is to scape the closing double quote of the onclick event. In this case I don't see any use in your calling. When used event functions in the html tags just like your onclick event, as html uses double quotes for the attributes you need to use the single quotes in your inline javascript functions.


By the way, have you checked about JQuery JavaScript Library. As you are starting coding javascript it's good to know your options.

Garis Suero
A: 

How about:

<a href="#" onclick="removeMap('test')" style="float:right; margin-right: 30px;"><img src="images/icons/collapse.gif"></a><br/>'
halfdan
A: 

You should use an editor that properly highlights strings (and what is going on should be obvious)

wybiral
+2  A: 

Or just stop writing javascript inside of your HTML tags, just call functions and put your code in your functions... far much readeable !

Julien Palard
A: 

Escaped quotes don't have any special meaning in HTML. Instead, the browser will see your line as:

<a href="#"
  onclick="removeMap('+ \"
  test\
  " +')"
  style="float:right; margin-right: 30px;"
>

Where the onclick would be broken Javascript, and there would be two attributes in the tag (test\ and " +')") that it doesn't recognize and doesn't know what to do with.

Daniel Vandersluis