3

is there a way with jTemplates to escape {$, so i can use inline javascript in my onBlur like

<a href="http://www.telegraaf.nl" onclick="if ( a ) {$('#something').css    ('display','none');alert('some msg');}">telegraaf</a>

which gets this after processTemplate:

<a onclick="if ( a ) " href="http://www.telegraaf.nl">

Thanks, Henk

EoghanM
  • 25,161
  • 23
  • 90
  • 123
Henk
  • 704
  • 6
  • 14

5 Answers5

8

jTemplates has a {#literal} ... {#/literal} tag that should prevent your curly braces from being affected.

<a href="http://www.telegraaf.nl" onclick="{#literal}if ( a ) {$('#something').css    ('display','none');alert('some msg');}{#/literal}">telegraaf</a>
great_llama
  • 11,481
  • 4
  • 34
  • 29
  • 1
    Thanks for answering the question instead of telling me better ways to do something. I know there are better ways, but sometimes that is not an option! This is exactly what I need, getting rid of tons of strings getting concatenated and put that stuf in a template!!! – Henk Aug 06 '09 at 19:56
3

Actually, in my opinion, I think its best to attach the event unobtrusively :

$(function () {
    $(".alink").click(function () {
        //if ( a ) {
            $('#something').css('display','none');
            alert('some msg');
        //}   
    });
});

<a class="alink" href="http://www.telegraaf.nl">
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 1
    My problem is that I have an old project full of things like c= ">input id ="; c+= other_stuff; etc, etc. this goes on for lots of lines of code I want to put all this html in templates, rewriting everythng is not an option (cost too much time) – Henk Aug 06 '09 at 12:20
  • Yes, I understand; I only provided you with my opinion, but yes, I understand that sometimes there are certain circumstances that get in the way of improving code quality – Andreas Grech Aug 06 '09 at 22:03
-1

If you're using jQuery then the $ is essentially just a shortcut to saying jQuery(expression) so in your case you can use:

<a href="http://www.telegraaf.nl" onclick="if ( a ) {jQuery('#something').css    ('display','none');alert('some msg');}">telegraaf</a>

You can read up on the selector shortcut at http://docs.jquery.com/%24

Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
-1

If you don't want to move your JS to separate secion or external file then you can always use jQuery "keyword" instead of $

<a href="http://www.telegraaf.nl" onclick="if( a ) {jQuery('#something').css('display','none');alert('some msg');}">telegraaf</a>

This way $ won't be interpreted as a template variable.

RaYell
  • 69,610
  • 20
  • 126
  • 152
-2
var test = function(el) {
   if ( a ) {
      $('#something').css('display','none');
      alert('some msg');
    }   
});

<a onclick="test(this);" href="http://www.telegraaf.nl">
andres descalzo
  • 14,887
  • 13
  • 64
  • 115