views:

594

answers:

5

Hi all,

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"&gt;

Thanks, Henk

+3  A: 

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"&gt;
Andreas Grech
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 codeI want to put all this html in templates, rewriting everythng is not an option (cost too much time)
Henk
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
A: 

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
Sorry, I know that, but jTemplates still doesn't like the {
Henk
A: 
var test = function(el) {
   if ( a ) {
      $('#something').css('display','none');
      alert('some msg');
    }   
});

<a onclick="test(this);" href="http://www.telegraaf.nl"&gt;
andres descalzo
A: 

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
Sorry, I know that, but jTemplates still doesn't like the {
Henk
+3  A: 

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
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