Render to clear textbox

There has to be a better way to do this. What I am trying is not working, and seems convoluted. Essentially, a controller method is called, and exits by rendering a partial the purpose of which is to simply clear a textbox in the view. So I am calling the partial which established the textbox – but it doesn’t work (i.e. it doesn’t clear the textbox). Does anyone know of a simpler way to do this that might work? Thanks, RVic, partial below:

<%= observe_field ‘channel_channelnote_note’, :url => {:controller => :channels, :action => ‘notesboxchange’ }, :with => “‘channelnotes=’ + value” %>

There has to be a better way to do this. What I am trying is not working, and seems convoluted. Essentially, a controller method is called, and exits by rendering a partial the purpose of which is to simply clear a textbox in the view. So I am calling the partial which established the textbox -- but it doesn't work (i.e. it doesn't clear the textbox). Does anyone know of a simpler way to do this that might work? Thanks, RVic, partial below:

I've read through this explanation and the code below twice, and I'm no closer to figuring out what you mean here. Can you give a slightly higher level explanation of what you're after here? I will be happy to help, but I don't understand what the trigger event needs to be to make the text field clear. You may be able to do all of this in JavaScript, or in a callback to another function (again, in JavaScript) that is doing the primary thing to make it necessary to clear this field.

So tell me a story like this:

  When the foo picker is set to baz, clear the bar text field.

Something like that, except what you really want.

Walter

Thanks Walter, and sorry for the confusion!

I have a textbox, on a page that also has a table that is constantly changing via AJAX. (So I dont want to just submit a form here)

Also on the page are some buttons. When the buttons are invoked a method in the controller is called (which uses the value in the textbox), and when the method exits, I need to clear the value in the texbox.

I am trying to do this all in the partial – both in the <input…value="" field as well as the javascript at the end of the partial which I assume is invoked as soon as the partial is loaded. Neither method seems to work, the partial is called when the buttons are clicked, the value in the textbox remains.

Thanks Walter, and sorry for the confusion!

I have a textbox, on a page that also has a table that is constantly changing via AJAX. (So I dont want to just submit a form here)

Also on the page are some buttons. When the buttons are invoked a method in the controller is called (which uses the value in the textbox), and when the method exits, I need to clear the value in the texbox.

I am trying to do this all in the partial -- both in the <input....value="" field as well as the javascript at the end of the partial which I assume is invoked as soon as the partial is loaded. Neither method seems to work, the partial is called when the buttons are clicked, the value in the textbox remains.

I don't know if/how this flag gets set in a jQuery Ajax request, but in Prototype, unless your entire response is in JavaScript (and thus is implicitly eval'd) you have to set a flag in the initial request to evaluate any inline scripts in the returned HTML content:

  new Ajax.Updater('table', '/path/to/method', { evalScripts: true });

If you enable that, then you can do something like this, inline after your table code is updated:

  <table id="table">
    ...
  </table>
  <script type="text/javascript">
    $('text_field').clear();
  </script>

The other way to do this is to return all of the data as JSON, and update the table and clear the field in one function in the base page.

Does that help?

Walter

Walter,

No, it occurs independent of the table actually. The Ajax that is in the page is not really even a aprt of the Rails app – it is json string fed to a peice of javascript on a timer that simply parses it into the table. The textfield, and having it clear when buttons invoke a rails controller method, is a different stunt I am trying to accomplish on the same page (independent of the ajax-driven table). Thanks.

Can you post a cut-down example on a jsfiddle?

Walter

Earlier you said

"... when the method exits, I need to clear the value in the texbox."

Which is it? If the former, and the result of the controller action is
irrelevant to the clear action, just set the value of the textbox to an
empty string on the click event -- simple, client-side.

I got it working by wrapping it as a CDATA in the partial, and making sure to put the ‘s’ in getElementsByName!

Has anyone ever worked out how to get javascript to re-execute on a reload event? Thanks for all your help with this guys.

document.addEventListener('DOMSubtreeModified', function(){
  // do whatever here
  // beware infinite loops if your callback modifies the DOM
});

Walter

Walter,

That’s very cool – but, how would i avoid an infinite loop? If I am clearing out an item that is an element of the DOM, withing your method, it will recurseively call it – how to prevent that

set a condition on your method, fail early.

if(elm.value == '') return;

Walter