1

I have a big doubt. In a MVC project, I render a HTML template (loaded from .html file) into a View with MvcHtmlString.Create();

This works fine but have a problem. This HTML template need to do javascript functionality, the problem? That this don't work, because the HTML is rendered after the javascript and the js functionality not work.

Something like this:

<script type="text/javascript">
$(document).ready(function(){
    $("#SomeID").click(funciton(){
        //This don't work because the #SomeID not exists yet 
    });
});
</script>
<br />
@MvcHtmlString.Create(@Model.HTMLTemplateContent);

Someone can help me about this or tell me something at respect?

Thanks

1
  • You misspelled the function keyword, 'funciton' won't work. Commented Nov 13, 2013 at 14:34

3 Answers 3

1

Use jquery on method.

$(document).ready(function(){
    $(document).on("click","#SomeID",function(){
        alert("Working");
    });
});

on method will work for current and future (Any thing injected to DOM later (Ex : through javascript code, ajax) elements.

Sign up to request clarification or add additional context in comments.

1 Comment

I suppose 'HTMLTemplateContent' is html markup, if so, it will render before document's ready event. While your solution might work, it may not be necessary.
1

Thanks! I not have idea about this.

Is possible to access at elements by the same "method"? How to do to get this? i.e

I'm using jSignature to implement sign en the document.

whit this:

$("#someDivID").jSignature();

I'm using now to access at div element with this:

var signCanvasOn = document.getElementById("someID");
signCanvasOn.jSignature();
signCanvasOn.jSignature("reset");

But this don't work for me, only work if I use:

$("#someID").jSignature();
$("#someID").jSignature("reset");

What is the real problem?

Comments

0

Have you tried adding the jquery at document level?

That's how i usually ensure that jquery continues to work with any stuff i load on the page dynamically.

Something like this:

  $(document).on("click","#SomeID",function()
  {
  //This don't work because the #SomeID not exists yet 
  });

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.