3

I'm stack in such question:

example

<td>
    <span style="cursor:pointer;" onclick="$(this).parent().html('<input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),'title' ,'3')}\'>');">
        <?=$result['settings_title']?>
    </span>
</td>

looks like fine but in browser I get "Uncaught SyntaxError: Unexpected identifier" in this row(chrome listing):

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),'title' ,'3')}\'>');">asdasd</span>

Please, help me - if somebody had such trouble before.Where am I wrong? Maybe will be better do it via PHP echo(I guess not:) )?

2
  • 2
    You should separate the JavaScript from the HTML. This nested event handler assignment can only call for quotes trouble. Looks like it's this part send_ajax_settings($(this),'title' ,'3'). Do yourself a favour and write maintainable code... Commented May 4, 2011 at 8:51
  • Thanks everybody and especialy Edgar Villegas Alvarado. Commented May 4, 2011 at 9:48

2 Answers 2

3

You didn't escape apostrophes in 'this' and '3'.
The right markup is this:

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),\'title\' ,\'3\')}\'>');">asdasd</span>

However, this kind of binding events is a very bad practice, and conduces to the type of mistakes you've just had. You should do it like this:

 <span style="cursor:pointer;" class="spanClass" data-param1="title" data-param2="3">asdasd</span>
 <span style="cursor:pointer;" class="spanClass" data-param1="title2" data-param2="5">asdasd</span>
 ... more spans generated dynamically

and have this:

   $(document).ready(function(){
     $(".spanClass").click(function(){
          var param1 = $(this).attr("data-param1");
          var param2 = $(this).attr("data-param2");
          $('<input type="text" value="asdasd" />').keydown(function(event){
            if(char_click(event)==13) {
               send_ajax_settings($(this), param1 , param2));
            }
          }).appendTo($(this).parent());
      });
    });

So, you can have javascript and html separated, and will work for any amount of elements generated dynamically by the server.

Hope this helps. Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

A very bad practice indeed. +1 for providing an answer with sanity!
3

I think you are still missing some escapes in the send_ajax function, try this:

<span style="cursor:pointer;" onclick="$(this).parent().html('input type=\'text\' value=\'asdasd\' onkeydown=\'javascript:if(char_click(event)==13){send_ajax_settings($(this),\'title\' ,\'3\')}\'>');">asdasd</span>

Alternatively to keep things more manageable, you can make the onclick call a function that executes the given code. This means you don't have to worry about escaping so much and should be a lot easier for you.

2 Comments

I thought about onclick function before - but decided to ask here first - to be sure that it is the best way to resolve this "magic" quotes.:)
@user I've posted a suggestion of a well structured code so you don't have those quoting problems again

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.