4

Inside jQuery.html(), I want to execute the following script.

$("#row1").html("<td id="cellId"><script>if (selectedStr) {var elem = $(\"#cellId\");       
$(elem).bind(\"onRefresh\", function() {loadColor(selectedStr); storeStr(selectedStr);});$(elem).trigger(\"onRefresh\");) }</script><td>")

Requirement:

I had a testPage.jsp which contains table with cells. When a particular cell is selected and "submit" button is clicked, a popup opens. In the popup page, some data is changed and is processed through ajax. The resultant ajax response has to be dynamically set to the parent row. The ajax response builds the whole content that is to be set.This is where I fail. The Ajax response will be something like this. <td>.....<td> <td>.....<td> <td>content<script>...</script></td> <td>.....<td> <td>content<script>...</script></td> ... Moreover the "selectedStr" is a javascript global variable in testPage.jsp.

Problems: 1. "selectedStr is undefined" is the browser error msg. I couldn't get reference to the global variable. 2. The element binding is not happening.

I tried to make use of jQuery.getScript(), jQuery.globalEval, jQuery.eval. I couldn't understand the {{html}} of JQuery.

Thanks!

3
  • 1
    i think you should take the if out the sentence, make all the procedures you need and finaly fill #row1 with the desired html code Commented Jan 10, 2012 at 15:58
  • 1
    The quotes in your HTML are escaped incorrectly...."cellId" Commented Jan 10, 2012 at 16:04
  • "cellId" escape was a typo error in the question. Commented Jan 10, 2012 at 16:14

4 Answers 4

3

Rather than put everything into a script, build your element in memory, then add the event handlers to it before adding to the DOM.

$td = $("<td></td>").attr("id", "cellId")
    .bind("onRefresh", function() {
        if (selectedStr) {
            loadColor(selectedStr);
            storeStr(selectedStr);
        }
    })
    .trigger("onRefresh");

$("#row1").html($td);

Also, be wary of adding multiple td elements with the same Id.

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

2 Comments

I am building the element data as part of ajax response. After that I am adding it to DOM. So the whole content you see in .html() is build dynamically.
That's fine, just add the code above to your success handler.
0

You should build html in that way:

var newRow = $('<tr/>').bind('onRefresh',function(){

{code}

});

$("#row1").append(newRow);

or

$("#row1").html(newRow);

but i really don't understand how exactly it should work on your page

1 Comment

Actually I am binding the event to <td>. so for every <td> I need to bind the event. Based on the "selectedStr" I will decide whether to bind the event or not.
0

If you are looking to fill the html based on javascript output you should use a function as the parameter to your jQuery.html(). See below.

$("#row1").html(function (index, oldHTML){
var elem = $(this);
//This is where you could run your if and other functions
//formulate the new html 
//then just return it to set the html
return newHTML;
});

Also, for more examples read the jQuery.html() api

Comments

-1

What I do when I need this is to define a hidden div in the dynamic content:

<div id="afterAjax" style="display:hidden">
if (selectedStr) {
  var elem = ... etc...
</div>

Note that I don't have a <script> tag.

Then, after ajax (or after you've otherwise inserted it into your HTML:

eval($('#afterAjax').html());

Update

Here's a fiddle that shows it working - dunno why someone marked my answer down. :( I use exactly this when I need to do it.

Code in the fiddle is just:

$('body').append('<div id="test">test</div><div id="afterAjax" style="display:none">alert("works");</div>');

eval($("#afterAjax").html());

http://jsfiddle.net/mhart/y22r5/3/

2 Comments

Thanks for your reply. But eval() is not working once the ajax response content is added to DOM. Should eval() be used inside <script>? $("#row1").html(ajaxRespContent); eval($("#afterAjax").html()); This is not working
Just tried it in a fiddle, works fine. Yes, eval() must be in your script after your load the response. jsfiddle.net/mhart/y22r5/3

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.