Search code examples
javascriptcode-snippetssublimetext-snippet

How do I create a snippet for jQuery?


So I want to create a snippet for the following:

$("input").click(function(event){
    //code goes here
});

But when I try to create the snippet in sublime text:

<snippet>
    <content><![CDATA[
$("${1:Tag}").click(function(event)
{
    ${2:code goes here}
});
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>sclick</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.js</scope>
</snippet>

Obviously there is some sort of conflict between the '$' of both. I read through the docs but didn't find anything. How do I create a snippet for this?


Solution

  • Use \ to escape the $:

    <snippet>
        <content><![CDATA[
    \$("${1:Tag}").click(function(event)
    {
        ${2:code goes here}
    });
    ]]></content>
        <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
        <tabTrigger>sclick</tabTrigger>
        <!-- Optional: Set a scope to limit where the snippet will trigger -->
        <scope>source.js</scope>
    </snippet>
    

    (It’s the only supported escape sequence.)