Assistance required in utilizing Objective C to run Javascript for automatically populating web forms

I've been grappling with this issue for a while now and I can't seem to make any progress. As someone who isn't well-versed in JavaScript, I've scoured various forums and websites for a solution but to no avail.

My current challenge involves populating a form using a webView in xCode.

The form is located on the website .

This is the markup of the form on the website:


             <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

Specifically, I am attempting to modify the text field with the title "zip search" and submit the form programmatically. However, my code doesn't seem to be working as expected - it doesn't even populate the text field.

    NSString* javaScriptString = @"document.getElementById('data[Search][zip_code]').elements.value='22911';";



[self.webView stringByEvaluatingJavaScriptFromString: javaScriptString];

Unfortunately, my attempts are futile. I have also experimented with GetElementsByName and various other methods without success.

Answer №1

Did you know that browsers allow you to input JavaScript directly into the URL bar? By loading the page you're linking to and entering a specific URL, you can see some interesting results:

javascript:alert(document.getElementsByName('data[Search][zip_code]'))

An alert message will show you what was found. Just remember to use the correct syntax like getElementsByName with an "s" for elements plural.

If you want to take it a step further, try this:

javascript:alert(document.getElementsByName('data[Search][zip_code]')[0])

This code will display an input element. And then:

javascript:alert(document.getElementsByName('data[Search][zip_code]')[0].value)

You'll be shown the current value of the input box. If you want to populate the box with a specific value:

javascript:document.getElementsByName('data[Search][zip_code]')[0].value = '22911'

Now you have the string you were looking for! Just remove the "javascript:" part.

Remember, if you make a syntax error in Mobile Safari, your statement might be ignored. And if you try to fetch something that doesn't exist, you'll receive a NULL message in an alert.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Animation issue in Material UI autocomplete label feature

Hello, I am currently delving into the world of React and Material UI. I have been working on implementing the Material UI auto complete feature with chip functionality. You can see my progress here: https://codesandbox.io/s/runh6. Everything seems to be w ...

`The best way to access a JavaScript variable from PHP`

This is the code snippet I am working on: var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; document.form1.bookingsession.focus(); var coords = { "lat": daaa }; $.get('bs.php', coords, function () { ...

Encountering an issue: Unable to load resources - Server returned a status code of 500

Struggling with implementing ajax code in my app development. I've encountered an issue where the ajax code that works fine in the video tutorials I'm following doesn't seem to work for me. It's really frustrating! Here is a snippet of ...

Analyze the elements within arrays and determine the occurrence rate of each element

Issue at hand: var rawData = [[two],[three],[four],[one],[two],[two],[two],[three],[four],[five],[three],[four],[five]]; Given an array of arrays, the goal is to identify the unique elements within one of the arrays and provide a count of each unique ele ...

Encountering a TypeError when using the react useRef() hook

During my work on an app utilizing the webRTC API and working with the useRef() hook in React, I encountered an error Error: TypeError: myVideo.current is undefined ContextProvider SocketContext.js:27 promise callback*ContextProvider/< SocketCon ...

What is the best approach for handling @RequestParam in a JSP file?

Hello there! I have a query regarding the usage of @RequestParam in @RestController. My question is about extracting @RequestParam from the client side. Below is an example of server code using @RestController: @ResponseBody @RequestMapping(method = Reque ...

What is the process for obtaining all of the options from a jQuery datepicker in order to create a new datepicker with identical settings?

Is there a way to easily replicate all options of a jQuery datepicker when creating a new instance? I am trying to duplicate a table that includes 2 datepickers with different settings. For reference, you can view an example here: http://jsfiddle.net/qwZ5 ...

What is the reason behind the necessity of adding an extra slash when reloading the page and controller in AngularJS by using $location.path()?

In AngularJS, when you use $location.path() and pass the same URL as the current one, it does not reload the page and controller. However, if you add an extra slash at the end of the URL like this: $location.path('/currentURL/'); it forces a re ...

Importing multiple modules in Typescript is a common practice

I need to include the 'express' module in my app. According to Mozilla's documentation, we should use the following code: import { Application }, * as Express from 'express' However, when using it in TypeScript and VSCode, I enc ...

Comparing two arrays of objects and updating the first object when a matching value is found in the second object: A guide

Trying to update the first array based on matching data from the second array. Encounter an error with forEach loop. First array: const data = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 3, name: "Charlie" } ] S ...

Observing the Result of a Function within a Controller Using Ng-Repeat in a Directive

I'm struggling with making a custom directive watch the result of a function that's bound to the scope in the controller. Here is the HTML. I'm passing the key of the ng-repeat to the function in the controller in order to determine whether ...

Using ReactJS, the for loop can be utilized to dynamically create buttons and assign variables based on the integer being iter

I need some help with ReactJS. I'm trying to generate 10 buttons using a for loop and assign a variable with the iteration number, but when I use the function setMqty(i), it always returns 11 for all buttons. Can anyone help me figure out the correct ...

When attempting to call Firebase Functions, ensure that Access-Control-Allow-Origin is set up correctly

Although it may seem straightforward, I am confused about how Firebase's functions are supposed to work. Is the main purpose of Firebase functions to enable me to execute functions on the server-side by making calls from client-side code? Whenever I t ...

Unable to save a URL link in MySQL database using JSON and PHP

I am seeking some insightful advice regarding my college assignment. This time around, I have to create yearbooks using Phonegap and it's my first experience working with AJAX, JSON, and JQuery Mobile. Instead of uploading an image directly into the ...

Issues with the functionality of the WordPress plugin

The issue with Spin360: All scripts are linked in functions.php add_action('wp_footer', 'add_scripts'); function add_scripts() { if(is_admin()) return false; wp_deregister_script('jquery'); wp_enqueue_script ...

Valums file-uploader: Restricting file uploads based on user's credit score

Currently utilizing the amazing file uploader by Valums, which can be found at https://github.com/valums/file-uploader One feature I am looking to incorporate is a limit based on the user's account balance. The initial image upload is free, so users ...

Calculating the sum of values in a specific position within an array of Javascript

Here is an array that needs to be updated: let arr = [ { "Id": 0, "Name": "Product 1", "Price": 10 }, { "Id": 0, "Name": "Product 1", "Price": 15 } ] I am looking for a way to add 1 to all the Price values, resulting in: let Final_arr = [ { ...

A guide on automating the html5 color picker using input type="color" in selenium webdriver

When interacting with an HTML color input element, a color picker window pops up that prevents viewing the DOM. I am looking for a solution to either select a color and close the window or enter a hex code in the popup's text box. Can anyone provide m ...

Using jQuery to retrieve the values of two distinct sliders and executing a specific mathematical operation

I have two sliders with their own values and properties: https://i.stack.imgur.com/3OZqr.gif My goal is to retrieve the values of these two sliders and calculate a result that will be displayed next to the interest label. The calculation formula is: poun ...

What is the best way to implement asynchronous image loading on hover events in React.js?

You may have come across this type of effect before. A good example can be found here - https://codepen.io/anon/pen/GEmOQy However, I am looking to achieve the same effect in React. While I understand that I can use the componentDidMount method for AJAX c ...