0

Ok, I am doing my nut trying to figure this out...

I have a page that pulls up a bunch of XML data and it is working fine.

I then decided to add a search box on this and other pages that will make this page only show the results of my search.

I know that the page is receiving the form data and that it is being correctly stored in variables.

I want to use those variables to then filter the results of my xml file.

What I have currently is;

var x=xmlDoc.getElementsByTagName("ResidentialProperty");
mySearch = location.search.substr(1).split("&")
function getFromSearch() {
  var x = 0
  mySearch = location.search.substr(1).split("&")
  for (x=0;x<=mySearch.length;x++) {
    eval("document.forms.myNewForm."+mySearch[x])
  }
}
document.write(mySearch);
for (i=0;i<x.length;i++) { 
  var item = x[i];
  document.write("<div class='property'><div class='list_name'>");
  if(item !== undefined) {
      var itemElements = item.getElementsByTagName("StreetNumber");
      if(itemElements.length > 0) {
          if(itemElements[0].childNodes.length > 0) {
              document.write(x[i].getElementsByTagName("StreetNumber")[0].childNodes[0].nodeValue+' ');
          }
      }
  }
  if(item !== undefined) {
      var itemElements = item.getElementsByTagName("StreetDirPrefix");
      if(itemElements.length > 0) {
          if(itemElements[0].childNodes.length > 0) {
              document.write(x[i].getElementsByTagName("StreetDirPrefix")[0].childNodes[0].nodeValue+' ');
          }
      }
  }

etc.

This is all working but every time I try to filter it just stops all results returning.

Please help.

1 Answer 1

1

Might be worth using a library like jQuery for this problem.

e.g. (taking a stab at the code without any testing)

var xml = $(xmlDoc);
var properties = xml.find('ResidentialProperty');

mySearch = location.search.substr(1).split("&");

var results = properties.filter(function() {
    return $(this).val().substr(0, mysearch.length) === mySearch;
}); 

console.log(results);

Reference

http://www.jquerybyexample.net/2013/02/jquery-parse-json-xml-html.html

https://api.jquery.com/find/

http://api.jquery.com/filter/

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

1 Comment

Thank you Sharn. I am trying to get it working with your help. No luck so far but I will keep plugging away.

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.