PDA

View Full Version : Passing user input to a jquery selector



tonybabb
03-03-2015, 03:37 AM
I'm creating a search page in a smartphone app, the search page accepts user input and then searches the app for a match and displays the results. For some reason the results come back as "undefined". I set up a jsfiddle to illustrate the problem here http://jsfiddle.net/tonybabb/Lvws4s0u/12/

I did some previous testing with the app using Alert to display the variable and that seems to be OK, I also hard coded the search term in the jQuery statement and that seems to work also so I think the problem is replacing the hard coded search term with a variable in a jQuery statement. Unfortunately I couldn't get Alert to work in jsfiddle so I removed them.

Thanks for any advice.

Tony

tonybabb
03-03-2015, 01:46 PM
Never mind, I figured it out. Not exactly sure why but the statement :

document.getElementById("searchResultsP").innerHTML = $( ".english:contains(" + $searchTerm + ")" ).html();
was inserting a bunch of js code in the paragraph. The resolution was to do it in two steps, first get the search results in a variable and then insert the variable in the paragraph as shown below

var $searchResultsAlert = $( ".english:contains(" + $searchTerm + ")" ).html();
document.getElementById("searchResultsP").innerHTML = $searchResultsAlert;