Topic: replace_html problem [solved?]

I decided to repost this over here, since its more appropriate:

Although I've seen the problem many places, I haven't hit on the right answer anywhere.  I have the following controller action:

def trans_item
  #params[:tDate]= '2011-02-13'
  #@transactions = Transaction.find(:all, :conditions =>["Transaction_Date = ?", params[:tDate]])
  respond_to do |format|

Note that at this point, I've commented out most of the code, just to elminate problems.  I have a trans_item.js.rjs file with the following:

page.replace_html('banner', '<p> This Test Works</p>')

However, all I ever get back from the Ajax request when I invoke the trans_item action is:

try {
Element.replace("banner", "<p> This Test Works</p>");
} catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.replace(\"banner\", \"<p> This Test Works</p>\");'); throw e }

Given the simplicity of what I'm doing, I don't get why this isn't working.

Just for fun, I pasted the javascript that the Ajax request receives in the responseText back into my javascript, and it works just fine.  I get no error messages anywhere, so why is the code not executed when its supposed to be?

Last edited by jleecbd (2011-02-15 14:58:03)

Re: replace_html problem [solved?]

Well, at least I think I solved it.  It occurred to me that I was calling the Javascript routine (as follows) from an option list, not a button:

function myAjaxTest() {
  var xhr = new XMLHttpRequest();"get", "/transactions/trans_item", false);


Although the impression I was left with was that the javascript generated by the rjs file would automatically be triggered, it occurred to me that might only be the case with buttons and links, not with element event triggers.  I put an eval(xhr.responseText) in, and the javascript is executed as expected.  Is my understanding of what went wrong correct?

Re: replace_html problem [solved?]

Now the next problem is that I'm wanting to replace the table rows with the newly rendered partial.  Because I've hard coded a date in the search request, I get back a single row, which is fine.  However, the row displays above the <div> (and above the actual table headers), and the <div> content on screen doesn't change.  At the same time, if I do an alert(myDiv.innerHTML), it shows the single row.

I'm getting the same behavior in both Safari and Firefox.  Also, I've tested it by manually doing a myDiv.innerHTML = <new html stuff>, as well as using the Prototype element.update.  The result is the same in both cases.