Split option value from ajax response

I would like to split a <option> so that i can use the value. I have googled for a way to do this but I cannot find an example. As I am fairly new to js and jquery, this is the first time I have had to find a way to do this. I have looked at the split() function, but am confused as to how this would work in my scenario.

What I would like to do as in my example is just assign the value to a variable. This is being returned by ajax from php.

<option value='123456789'>123456789</option>
I would be grateful if someone could help me with this. Many thanks

Hi poorbaldrick I think the easiest way to do this would be taking advantage of the JavaScript DOM API… you could do something like this:

var htmlString = "<option value='123456789'>123456789</option>";
var node = document.createElement('div');
node.innerHTML = htmlString;
var option = node.querySelector('option');
var value = option.value;
1 Like

Andres, thanks for reply. Would it also work using the code below bearing in mind that this value could change and data holds the returned value from php. Thanks

var htmlString = data;

Yes it should work if data is an HTML string… also depending of what HTML it contains you might have to modify the script to select what you’re after accordingly… If you post a sample payload we might be able to help you further.

OK. many thanks for your help.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.