Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6019847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:28:49+00:00 2026-05-23T03:28:49+00:00

I want to emit some js from jQuery templates e.g I have template like

  • 0

I want to emit some js from jQuery templates e.g I have template like

<script id="tmplID" type="text/x-jQuery-tmpl">
<input id="static.${val}" type="text"/>
<input id = "static2.${val}" type="text"/>
<script type="text/javascript">
$('#static.'+${val}).change(function(){
  alert('some value');
});
</script>
</script>  

When I call this template like:

var obj = {val:'123'};
$('#tmplID').tmpl(obj).appendTo("some_container");

it renders the JavaScript change function as string on the page whereas I want it to be appended as executable JavaScript.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:28:50+00:00Added an answer on May 23, 2026 at 3:28 am

    There are multiple issues with your code.

    First of all, you should never use nested <script> tags. Even if the closing </script> tag is inside a string in a javascript block you should still split it so it doesn’t appear as is, as it could and will trip even the most standards compliant browsers. Not to mention the older ones…

    Similarly, you should avoid using dots in IDs, as it is very easy to overlook when writing your jQuery selectors. #id.something means “select the element with the id id which also has class something applied to it”. If you can’t live without it, you can escape it when writing your selector like #id\\.something, which will “select the element with the id id.something“.

    It’s also a bad idea to put javascript inside your template, you should re-think what you’re trying to achieve. There is almost always a way around it:

    • You can bind your events with .delegate() or .live() to elements which you will only create in the future.

    • Instead of hard-coding the ID in the script, you can use different selectors, e.g. you can apply a unique class to use with the above bindings so it won’t interfere with existing markup. If you need the ID inside the event handler, you can easily extract it with this.id or $(this).attr('id').

    • If you want to customise 'some value' in your script, you can add custom attributes to your elements, e.g. <input id="static_something" data-custom="some value" /> and extract it in your function using $(this).data('custom').

    Taking all of the above into account, I would re-write your code to something similar to this:

    <script id="tmplID" type="text/x-jquery-tmpl">
    <input id="static_${val}" class="templateinput" type="text" data-custom="value1" />
    <input id="static2_${val}" class="templateinput" type="text" data-custom="value2" />
    </script>
    
    <script type="text/javascript">
    // jQuery ready function
    $(function(){
        $('.templateinput').live('change', function(){
            var element_id = this.id;
            var element_data = $(this).data('custom');
    
            alert('The value of input #' + element_id + ' is: ' + element_data);
        });
    });
    </script>
    

    If you have a known container which will contain all your template items, use .delegate() instead for clarity to avoid global binding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm defining some types via System.Reflection.Emit. Imagine that I want to have method signature
I want to emit dynamic error messages like all interpreters do nowadays, for example:
want to know why String behaves like value type while using ==. String s1
I have some javaScript Classes ( ctor+prototype methods) that I want their instances to
I have some simple javascript that I'd like to run when a button is
I want to have a dialog window where some buttons close the dialog and
I am using System.Reflection.Emit, and at some point I want to create a delegate
I'm trying to emit some jQuery and other Javascript that will hide and show
want to have a Hyperlink-Button in a gridView in which I can display a
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.