ismail baig
ismail baig

Reputation: 891

An html tag whose ID is having JSRendered Value, its value reading in the jquery script is not happening?

am using JSRender in my application.

<html> 
<body> 
    {{for Comments}}  
        <input id="id_{{:CommentId}}" type="hidden" value="-1" />
        <textarea id="cmt_{{:TopicId}}" ......... />
    {{/for}}
</body>
</html>
<script type="text/javascript">
 $(document).ready(function () {

  //The below code gets executed / works fine.
  $('#cmt_{{:TopicId}}').bind('keyup keydown paste', function (e) {
    //my logic        
  });

  //This code doesnot work fine, instead of -1, it gives undefined.
  var rankAction = $("#id_{{:CommentId}}").val();
});        
</script>

i want the rankAction variable to give -1, whereas it returns undefined, why is it so?

Is it simply because id containing JSRendered value within them cant be read in Script coding?

Then why is the other part i.e. cmt{{:TopicId}} returning a value ?

Where is it wrong?

Upvotes: 1

Views: 358

Answers (1)

pedz
pedz

Reputation: 2349

To stack overflow folks: I apologize for making this an "answer" but the comments are not powerful enough to describe my questions.

@ismail baig: A jsrender template is called such as this:

// Render the template with the movies data and insert
// the rendered HTML under the "movieList" element
$( "#movieList" ).html(
    $( "#movieTemplate" ).render( movies )
);

Note the pattern is some template.render(datalist) where the template can be defined using:

<script id="movieTemplate" type="text/x-jsrender">
    <div>
        {{:#index+1}}: <b>{{>name}}</b> ({{>releaseYear}})
    </div>
</script>

#movieList is just an element. movies is a javascript array. I don't see those three items: the template, the data list, nor the call to render the template in your example.

For reference, look at this page and do a view source to see the code, template, and usage.

Upvotes: 1

Related Questions