I have been stressing about this for a few hours and I cannot get it working. I have no experience in javascript and have been searching around in the forum and other sites, but I can’t seem to get a solution that works.
I am trying to fill in the form using a webView in xCode
The website http://www.eatwellguide.org/mobile/ is where the form is located.
The website has this forum
<form name="data[Search][form]" METHOD="post" ACTION="/search/advanced/find" ID="frmAS" >
<ul>
<li><input type="hidden" name="device" id="device" value="mobile" title="mobile">
<input type="hidden" name="iframe" id="iframe" value="11" title="mobile" > Keyword: </li>
<li><input type="text" name="data[Search][keyword]" id="SearchKeyword" title="search by keywords" />
<input ID="SearchSubmit" class="BTNsend" type="submit" value="Find" name="data[Search][submit]"/></li>
<li>Zip / Postal Code:</li>
<li><input type="text" title="Zip search" value="" name="data[Search][zip_code]" /> <input ID="SearchSubmit" class="BTNsend" type="submit" value="Find" name="data[Search][submit]"/> </li>
<li><select name="data[Search][distance]" style="font-size:120%; ">
<option value="1">1</option>
<option value="5" selected="">5</option>
<option value="10">10</option>
<optionvalue="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="200">200</option>
</select> <input type="radio" ID="SearchDistanceUnit" value="Mi" checked="" name="data[Search][distance_unit]" class="BTNradio"/> Mi
I am trying to modify the textbox that i has the title = “zip search” and submit the form, but my code doesn’t work at all. It won’t even fill in the textbox
NSString* javaScriptString = @"document.getElementById('data[Search][zip_code]').elements.value='22911';";
[self.webView stringByEvaluatingJavaScriptFromString: javaScriptString];
It just does nothing. I have also tried GetElementsByName and many other variations of the two.
Browsers support the input of JavaScript directly into the URL bar. So if you load the page you link to, then enter the following URL:
You’ll get an alert message showing what was found. In that case I’ve taken your
getElementByNameand corrected it — elements should be plural and it returns an array.Building up:
Evaluates to show an input element. Then:
Shows the current value, and:
Populates the box. So that’s the string you want (minus the
javascript:).If you’d taken a syntactic wrong turn, at least in Mobile Safari, your statement would be ignored. If you’d ended up trying to fetch something that isn’t there you’d have received a NULL message in an alert.