Forum Moderators: open

Message Too Old, No Replies

jQuery postback/live problem

jQuery postback/live problem

         

Dunce

7:50 am on May 17, 2011 (gmt 0)

10+ Year Member



Hi,

I have a jquery postback/live problem if anyone can offer any pointers..

I have a main page which has click events that creates a div as modal window.
The modal window posts back to itself and performs server side processing along the way
If the results of the server side processing are successful, I would like to update a div (or call a function) in the main page.

The problem is that any javascript that's added dynamically after the postback, refuses to interact with existing elements (of functions) from the main page (or dom).

There are no events such as 'click' that I can use to bind to with .live, so how can I get this newly added javascript to update elements from the main page?

many thanks for any pointers,

Mark

astupidname

10:42 am on May 17, 2011 (gmt 0)

10+ Year Member



Not entirely sure if I'm reading the question right or not, but the issue is, I suspect, in how you may be adding the javascript to the page. If you are just adding scripts via string to some elements innerHTML or something, that's a no-no. Rather use DOM methods, along the lines of:

var script = document.createElement('script');
script.type = 'text/javascript';
script.text = 'alert("hello world, from dynamically added script");';
document.body.appendChild(script);

If that's not the issue, perhaps you can clarify with some code and/or more details.

Dunce

10:59 am on May 17, 2011 (gmt 0)

10+ Year Member



Thanks for the reply, very much appreciated.

The script is being added to the page directly within script tags, within the returned content i.e.


<script type="text/javascript">
$(function(){
$("#myDiv").append("success");
//myDiv is in the main page
});
</script>


It only gets added after succesful server side processing of the data.

Hope that makes sense

Dunce

3:45 pm on May 17, 2011 (gmt 0)

10+ Year Member



Solved.

The ideal solution here for me would have been to use .live with a 'load' event (which of course is not possible) to load any newly added script from the postback.

I ended up using a callback event to look for a hidden DIV that will be planted in the page using server side code.

If the div exists, the callback takes the contents of the div (javascript code) and adds this to the existing script - works a treat.

Thanks for your suggestion, it prompted the solution

Mark