0

I am making an AJAX call that returns multiple records from my db each enclosed by <pre> tags like so:

  <div id = "ajaxdiv">
  <pre id = "1"> record 1 </pre>
  <pre id = "2"> record 2 </pre>
  etc. etc.
  </div>

I want to be able to selectively hide one or more of the records returned so only the ones most pertinent to the original query remain. The number of records returned can vary. Javascript inserted as DOM text will not execute so I am a bit of a loss how to accomplish this using JQuery hide() etc.

Any help would be appreciated.

user1884367
  • 387
  • 2
  • 6
  • 15
  • `$(ajaxResponse).find("#1").hide()` ? – hawk Aug 01 '14 at 03:38
  • 1
    I think your best option is to learn more about javascript and how to make it execute dynamically. Using an ajax call to dynamically load javascript (assuming that's what you implied by `Javascript inserted as DOM text will not execute`) is not considered good practice. – Cody Aug 01 '14 at 04:07

1 Answers1

0

Upon obtaining the response from AJAX, you can render the records as you are now doing. Then you can create an array that contains the id to be hidden.

// add something like this to your code
// after the results has been rendered.
var idsToHide = [ 1, 3, 5];

idsToHide.forEach(function(elementId) {
    // hide the element via jQuery
    $('#' + elementId).hide();
});

See http://jsfiddle.net/jQU2B/2/

khakiout
  • 2,285
  • 21
  • 30
  • I thought that calling a script embedded in an AJAX response was not possible since the response is not part of the DOM. Am I missing something? – user1884367 Aug 01 '14 at 04:12
  • You call this after the AJAX response has been retrieved and rendered. Also, take note of what Cody mentioned in the question comments. – khakiout Aug 01 '14 at 04:51
  • Your point regarding good practice is well taken. I have been able to implement what I want using the JQuery-UI Accordion widget. The only problem is that Accordion by default only displays one panel. I would like the default to be all panels open with the ability to close one or more panels. Any ideas? – user1884367 Aug 01 '14 at 14:58
  • @user1884367 Please refer to this [answer](http://stackoverflow.com/a/12903079) for opening all panels by default. – khakiout Aug 01 '14 at 17:31