3

I am fairly new to snippet creation. I have successfully created multiple snippets for C# and HTML. When I ran into troubles I started using the MS Snippet Designer. The trouble I had was when I created a snippet for JS using the following code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>tjready</Title>
      <Author>admin</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>$().ready(function() {</Shortcut>
    </Header>
    <Snippet>
      <Code Language="javascript"><![CDATA[$().ready(function() {
//This is a test of the onready
    });]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

My goal is to create the snippet for either JS or CSHTML files. The above does not work in either type of file after an import using the Code Snippet Manager (Tools > CSM). I tried placing the snippet in both the html and JavaScript categories. I also tried modifying the language type of the snippet.

Does anyone see what I am doing wrong?

2 Answers 2

6

For anyone else that comes across this as well, if you need to add a leading $, add two.

<Snippet>
  <Code Language="javascript"><![CDATA[$$().ready(function() {//This is a test of the onready    });]]></Code>
</Snippet>
Sign up to request clarification or add additional context in comments.

2 Comments

Good point, I did not mention that, I think I learned that the $ is an escape character later on. So the first $ escapes the jQuery $. Thank you for clarifying.
As far as VS2022 17.5.1, this solution works.
0

I found the issue and a solution. The issue was the leading $. Once I took that off I could create the snippit using ctrl K + ctrl X. I wanted to use the shortcut type and auto fill though.

Since I could not get the type and tab auto fill to work I continued to look. I found a Visual Studio jQuery snippet plugin that works for what I am trying to do and comes with several snippets ready to go.

The jQuery snippet shortcut on this is jqDocReady or you can use jqDocReadyShort if you do not want the anonymous function in there.

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.