1

I have the following code which appends an icon with an alert after the <a> tag :

$j("li[name='"+node_name+"'] > a").after('<a onmouseover="alert(\' expnote_from_db[n][0] \');" ><ins class="' + selected_class + '">&nbsp;</ins></a>');

The code above works fine except that it displays exp_from_db[n][0] as a string inside the alert box.

So I changed it to the code below but nothing is displayed now

  $j("li[name='"+node_name+"'] > a").after('<a onmouseover="alert(\'"'+ expnote_from_db[n][0] + '"\');" ><ins class="' + selected_class + '">&nbsp;</ins></a>');

I don't understand where did I go wrong with the apostrophes.

I would appreciate your help regarding this. Thanks

1
  • "alert(\' expnote_from_db[n][0] \');" should look like "alert(\'' + expnote_from_db[n][0] + '\');" Commented Aug 31, 2012 at 14:07

2 Answers 2

3

This should work

$j('li[name="'+node_name+'"] > a')
    .after('<a onmouseover="alert(\''+ expnote_from_db[n][0] + '\')" ><ins class="' + selected_class + '">&nbsp;</ins></a>');
Sign up to request clarification or add additional context in comments.

Comments

3

The " characters are delimiting the HTML attribute. You are terminating that attribute prematurely.

<a onmouseover="alert(\'"

Nesting JavaScript in HTML attributes is a pain.

Nesting JavaScript in HTML attributes in JavaScript strings is a bigger pain.

Don't do it. Apply event handlers using addEventListener and friends (since you are using jQuery that means using the on method which abstracts them).

Comments

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.