-1

I'm having trouble passing apostrophe's into a string. Everything I try (escaping etc) freaks out.

Could you advise me how to output the following line in the browser :

<a href="javascript:;" onclick="addToBasket('tk_id','track')" class="pl-buy"></a>

Using the following format :

d += "<a href='javascript:;' onclick='addToBasket("' + a.tk_id + '" , 'track')' class='" + this.options.playlistOptions.plbuybutton + "'></a>";

I know this isn't the best way to do it but if it's possible I'd like to do it this way without having to write a function.

Thanks in advance,

Grant

Grant
  • 1,147
  • 1
  • 13
  • 31

5 Answers5

1

Here:

d += "<a href='javascript:;' onclick='addToBasket('" + a.tk_id + "', 'track')' class='" + this.options.playlistOptions.plbuybutton + "'></a>";

You had a few single/double quotes round the wrong way.

dsgriffin
  • 61,907
  • 17
  • 128
  • 134
  • @Grant What are you returning? because my answer works. Check here - http://jsfiddle.net/ZNzUL/. If your variables are returning extra quotes which mess this up, then your problems are with your variables themselves, not this. – dsgriffin Jun 03 '13 at 20:47
  • Ok this is where I'm going to annoy you ;) It's some add on code that I've cobbled together as part of JPlayer's Playlist code. To be honest I'm just winging it, i'm a bit lost when it comes to JQuery & Javascript. If you can tell me how to paste code in a reply here I'll paste a chunk of it and you may be able to see where i'm going wrong? – Grant Jun 03 '13 at 20:52
  • @Grant As long as you accept my answer afterwards ;). Please make a jsFiddle for me with your HTML/jQuery which shows me your problem - **http://jsfiddle.net/** – dsgriffin Jun 03 '13 at 20:54
  • @Grant Have you noticed the typo error? line 49, global.js - d(f.jPlayer.status.media); - d is undefined – dsgriffin Jun 03 '13 at 21:14
  • I hadn't noticed that but that is just loading and switching the waveforms in the player and that all seems to be working just fine. – Grant Jun 03 '13 at 21:17
  • Nope - that's returning **** If it helps I've fiddled the ajax code that will eventually be receiving the link in question. Maybe it can be formatted differently? http://jsfiddle.net/7kDP2/ – Grant Jun 03 '13 at 21:29
  • @Grant BTW, have you console logged both variables to make sure they aren't returning any extra quotes with them? – dsgriffin Jun 03 '13 at 21:30
  • @Grant Please try this, it'll remove all single/double quotes - `d += "";` – dsgriffin Jun 03 '13 at 21:32
  • Tried that one and it's still putting out mad stuff :( – Grant Jun 03 '13 at 22:35
0

Perhaps a template lib comes in handy - like:

https://github.com/janl/mustache.js

http://handlebarsjs.com/

or even: http://underscorejs.org/#template, which is not really a templating library, but a nice swiss-army-knife general purpose lib with templating gratis.

Thomas Junk
  • 5,296
  • 2
  • 25
  • 39
0

Escaping with backslash always worked for me:

alert('\'abc\'');

In your case it's:

d += '<a href=\"javascript:;\" onclick=\"addToBasket(\'' + a.tk_id + '\',\'track\')\" class=\"' + this.options.playlistOptions.plbuybutton + '\"></a>';
David Jashi
  • 4,330
  • 1
  • 19
  • 26
  • Escaping just causes weird characters to appear in the output in this case. – Grant Jun 03 '13 at 20:41
  • How come? All I get is a popup window with `'abc'` in it. – David Jashi Jun 03 '13 at 20:47
  • Well, and what else do you want from it? I pressed 3 links on the left, it's playing, outputs some info, nothing looks wrong. Maybe you should clean up your browser cache? – David Jashi Jun 03 '13 at 21:01
  • `` - this is what is generated. Something is messed up in `a.tk_id` variable, that's a problem. – David Jashi Jun 03 '13 at 21:06
  • The actual variable is outputting just fine though! It's seems to be the fact that I need to output 2 sets of vars contained in single apostrophe's ('var1','var2') in the string that's causing all the fuss. – Grant Jun 03 '13 at 21:14
  • if I knew how to use both variables I wouldn't be here begging for help ;) – Grant Jun 03 '13 at 22:37
  • Jesus Christ and Virgin Mary... Update question with a piece of code, where `a.tk_id` is assigned with a sample value assigned to it. I'll try to work something out. – David Jashi Jun 04 '13 at 05:06
0

After about 5 solid hours of trying to figure this out I thought i'd try a different approach and ended up simply using the ASCII character for the single quote's (not apostrophe's as I incorrectly called them earlier) and it worked like a charm ;)

Here is the final working code (ugly but it does the job) :

d += "<a href='javascript:;' onclick='addToBasket(&#39;" + a.tk_id + "&#39;,&#39;" + a.type2 + "&#39;)' class='" + this.options.playlistOptions.plbuybutton + "'></a>";

Thanks so much to everybody that offered their help.

G ;)

Grant
  • 1,147
  • 1
  • 13
  • 31
0

to do simple use escape function like that :

escape("let's go");
JFlow
  • 1
  • 1