XML Based Search (HTML) Javascript) (Dom) (XML) (Search)
XML Based Search (HTML) Javascript) (Dom) (XML) (Search)
DZone Snippets
Search
Popular @ dzone.com
IonMonkey: Mozilla’s new JavaScript JIT compiler
// The javascript:
window.onload = loadIndex;
if (results.length > 0) {
// if there are any results, put them in a list inside the "resultshere" div
var resultshere = document.getElementById("resultshere");
var header = document.createElement("h5");
var list = document.createElement("ul");
var searchedfor = document.createTextNode("You've searched for "+searchterm);
resultshere.appendChild(header);
header.appendChild(searchedfor);
resultshere.appendChild(list);
for (var i=0;i<results.length;i++) {
var listitem = document.createElement("li");
var item = document.createTextNode(results[i].lastChild.nodeValue);
list.appendChild(listitem);
listitem.appendChild(item);
}
} else {
// else tell the user no matches were found
var resultshere = document.getElementById("resultshere");
var para = document.createElement("p");
var notfound = document.createTextNode("Sorry, I couldn't find anything like "+searchterm +"!");
resultshere.appendChild(para);
para.appendChild(notfound);
}
}
// Here's some s(a|i)mple HTML that should work with the code above:
<html>
<head>
<script type="text/javascript" src="searchindex.js"></script>
</head>
<body>
<form action="">
<input type="text" id="searchme" />
<input type="submit" onclick="searchIndex(); return false;" />
</form>
<div id="resultshere">
</div>
</body>
</html>
to html javascript dom xml search by Wieland on Tue Sep 26 09:35:26 -0400 2006
Thanks,
Mike
fatmess posts on Jan 24, 2009 at 09:03
Thank you so much for posting this code snippet.
I am very much a beginner at this.
How might the .js code be adapted so that other child elements are retuned along with <item>
ie.<age> <height> <weight>
thanks again!
kboddy posts on Jun 26, 2009 at 14:24
Has anyone answered the previous comments? I need to do the same and haven't been able to figure it out as yet. Any help would be much appreciated.
Thank you,
Kboddy
mapmaker posts on Jul 07, 2009 at 00:40
Hello- I've hacked this code a bit to write the search results, including xml attributes to an html table.
if (results.length > 0) {
// if there are any results, write them to a table
} else {
// else tell the user no matches were found
var notfound = alert('No results found for '+searchterm+'!');
}
}
</script>
</head><body>
<form action=""><b>Search: </b><input id="searchme" type="text" size="20"> <input value="Submit" onclick="searchIndex(
</body></html>
Hope this helps!
xcitrate posts on Jul 26, 2009 at 11:03
Thanks a lot guys!! this script help me a lot on my data work, and havent ever seen before on any page on the web!! but now my Question is, how can i retreive an
image from the xml,, just like a profile pic for each data line..
eg. ?name=paul&weight=168
<body>
<form action="">
Thanks in advance!
xcitrate posts on Jul 26, 2009 at 22:05
yep i got the images!! but not the two values search match.. anyone knows!!
window.onload = loadIndex;
if (results.length > 0) {
// if there are any results, write them to a table
} else {
// else tell the user no matches were found
var notfound = alert('No results found for '+searchterm+'!');
}
}
</script>
</head><body>
<form name="frmMain" id="frmMain" action="">
<b>Search: </b><br><br>
<input type="radio" name="criteria" value="name" checked="checked">Name
<input type="radio" name="criteria" value="age">Age
<input type="radio" name="criteria" value="height">Height
<input type="radio" name="criteria" value="weight">Weight
<br><br>
<input id="searchme" type="text" size="20"> <br><br>
<input value="Submit" onclick="searchIndex(); return false;" type="submit">
</form>
</body>
</html>
Hope this helps anyone who wanted to achieve the same effect.
emily_raw posts on Sep 01, 2009 at 19:24
Any ideas on how to make the search results links?
i.e. my search is for documents so I want to be able to have links on the results page
e.g.
<item name="document" department="HR" download="link">John</item>
Shaun_ZA posts on Mar 31, 2010 at 07:50
I like the code above, but I have a slightly different requirement.
Loading the XML, then using a form with searchterm of "customer".
The code is very similar, but each entry in the XML is different. using <customer>Some customer</customer>
1. When I search for something that may be listed in the XML, then I want an alert box that will say ("+Searchterm+ is found" or ("+Result+ is found")
I do not want it to display a new page with the result in, only an alert that will say no it's not found, or yes it is found.
2. Another thing is a custom alert, or popup. If possible a red cross instead of a triangle-with-exclamation when not found, and a green tick when found, with the
appropriate text and button of course.
// send the results to another function that displays them to the user
showResults(results, searchterm);
}
}
if (results.length > 0) {
for (var i=0;i<results.length;i++) {
var Message = " is a Customer" + 'n'
+ "Call the Service Provider" + 'n'
+ "or re-type your search criteria"
var found = alert(+results+ 'Message');
}
} else {
// else tell the user no matches were found
var Message = " is not a Customer";
var notfound = alert(searchterm.toUpperCase() + Message);
}
}
mike248x posts on May 08, 2010 at 10:06
Hey guys, I'm very new to this. Can someone please tell me how to hyperlink the results using the first example xml and the Javascript on the top of the page? Any help
will be sincerely appreciated. Thanks in advance.
blencorp posts on May 10, 2010 at 17:04
@mike248x and others... I've re-wrote the script above using jQuery framework. Here is a discription and full code
window.onload = loadIndex;
// send the results to another function that displays them to the user
showResults(results, searchterm);
}
}
function showResults(results,searchterm) {
if (results.length > 0) {
for (var i=0;i<results.length;i++) {
var newResults=string (results.value)
Message = " is an SPC Customer" + 'n'
+ "Call SPC" + 'n'
+ "or re-type your search criteria"
alert(newResults+ Message);
}
}
else {
// else tell the user no matches were found
Message = " is not an SPC Customer";
alert(+searchterm+ Message);
}
}
<Example>
<User>
<ID>95</ID>
<Age>29</Age>
<Name>Robert Hassen</Name>
<City >Paris</City >
<Nickname>usr93</Nickname>
<FileNames>
<FileName>Documents\Abcd.pdf</FileName>
</FileNames>
</User>
</Example>
Related Posts
» split value from units in html javascript dom formatting
» change text in html javascript dom text
» change title with javascript in html javascript dom title
» Firefox 3 and input file in html javascript firefox dom
» Php xml Parser in c ruby rails sql html javascript linux apache unix osx windows date python hash regex css mysql php bash perl java string image xml find convert
shell series60 database xslt web array url c++ time rubyonrails http google text activerecord rexml math csharp file REBOL jonas jsfromhell raoni sinatra rscript
» CSS Rounded Corners in c ruby rails sql html javascript linux apache unix osx windows date python hash regex css mysql php bash perl java string image xml find
convert shell series60 database xslt web array url c++ time rubyonrails http google text activerecord rexml math csharp file REBOL jonas jsfromhell raoni sinatra rscript