I've been trying to get this to work for several days without success, I'm probably missing something very obvious.
Basically I see 3 problems - so far!
1) I'm not able to replace a hard coded search term with a variable in a jQuery statement
2) When the hard coded search runs it returns the correct phrase but also includes a comment from the original html
3) It only returns one search result when it should be returning several
For testing I have been entering "arrest" in the search box.Here's what I've got so far:
Below is a section of the html content it is searching
Code:
<div class="english"> <!-- Start English -->
You are under arrest
</div> <!-- End English -->
<div class="wrapper">
<a class="formal spanish" data-ignore="true" href="arrUnderArrestF.mp3">F: Está bajo arresto</a>
<a class="informal spanish" data-ignore="true" href="arrUnderArrestI.mp3">I: Estás bajo arresto</a>
</div> <!-- End .wrapper -->
<div class="english"> <!-- Start English -->
I have an arrest warrant
</div> <!-- End English -->
<div class="wrapper">
<a class="spanish" data-ignore="true" href="arrArrestWarrant.mp3">Tengo una orden de arresto</a>
</div> <!-- End .wrapper -->
I have a Search page where the user enters their search term and the results of the search should be displayed, here's the code for the page
Code:
<div class="english">
Search for words in English
</div>
<div class="spacer"></div>
<input name="searchTxt" type="text" maxlength="32" id="searchTxt" class="searchField"/>
<button onclick="searchApp()"> Search </button>
<div class="spacer"></div>
<div id="searchResultsDiv">
<p id="searchResultsP">Search results go here</p>
</div> <!-- /searchResultsDiv -->
and here's the js with some comments added
Code:
function searchApp() {
var $searchTerm = "";
$searchTerm = document.getElementById("searchTxt").value;
alert ("$searchTerm is " + $searchTerm); // this displays the search term entered by the user and is working correctly
alert("search 1 results " + $( ".english:contains($searchTerm)" ).html()); // this does not work, returns 'undefined'
alert("search 2 results hard coded " + $( ".english:contains('arrest')" ).html()); // this works correctly - I just hard coded a search term
var $searchResultsAlert = $( ".english:contains('arrest')" ).html();
alert ("search 3 results hard coded are " + $searchResultsAlert); // this works correctly
// The next line should overwrite the paragraph on the search page with the search results
document.getElementById("searchResultsP").innerHTML = $( ".english:contains('arrest')" ).value; // This does not work it returns 'undefined' in the paragraph
} // /searchApp
Thanks for any input you may have.
Tony
Bookmarks