0

I am trying to make a dynamic HTML which I'll append using jquery before append if I'll console the HTML everything looks fine but after append in browser The whole structure messed up.

Here is my HTML :

<script>
var title = "my title";

var toolbar ='<hm_toolbar user="current_user " upp="hm"  index="hmIndex "></hm><synapse_toolbar uuid=" hm.hm_pk " my-method="get_linked_facts" ng-if=" flag == true "></synapse_toolbar>';

var map_html = '<a onclick="panToMarker(' + lat + ',' + long + ')"> ' + title +'"</a>' + toolbar ;

var li_html = "$('#" + title + "').append('<li class=\"list-group-item\"><div dynamic=\" " + map_html + " \"></div></li> ')" ;

var g =title.replace(/ /g,"_");;

var fn = "function "+ g +"(){ console.log('--working--'); "+ li_html +"; }";
    console.log(fn)
   eval(fn);
  }
  </script>

When the above li_HTML does its work means append the HTML the appended is all messed up

Appended HTML :

<li class="list-group-item"><div pantomarker(49.711083,6.251445)"="" dynamic=" &lt;a onclick="> Test"<hm_toolbar index="hmIndex " upp="hm" user="current_user "></hm_toolbar><synapse_toolbar ng-if=" flag == true " my-method="get_linked_facts" uuid=" hm.hm_pk "></synapse_toolbar> "&gt;</div></li>

I know I have messed up the concatenation using with quotes but I am not able to fixed the bug .

3
  • try escaping your ' characters inside the string. do it like: var li_html = "$(\'#" + title + "\').append(\'<li class=\"list-group-item\"><div dynamic=\"" + map_html + " \"></div></li> \')" ; Commented Sep 30, 2015 at 9:03
  • @Vance nop , no luck Commented Sep 30, 2015 at 9:06
  • @saf there is the " quote is mising at var map_html = '<a onclick="panToMarker(' + lat + ',' + long + ')"> ' + title +'"</a>' + toolbar ; i think that is making problem Commented Sep 30, 2015 at 9:43

1 Answer 1

1

You said that the console.log() looks OK but you have eval() after that which means that you have to escape some quotes twice.

Try this:

var toolbar ='<hm_toolbar user=\\\'current_user \\\' upp=\\\'hm\\\'  index=\\\'hmIndex \\\'></hm><synapse_toolbar uuid=\\\' hm.hm_pk \\\' my-method=\\\'get_linked_facts\\\' ng-if=\\\' flag == true \\\'></synapse_toolbar>';

var map_html = '<a onclick=\\\'panToMarker(' + lat + ',' + long + ')\\\'> ' + title +'</a>' + toolbar ;
Sign up to request clarification or add additional context in comments.

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.