I have part of code:
await fetch(request)
.then((response) => {
if (response.status === 200) {
console.log("Success");
result = response.text();
} else {
throw new Error("Something went wrong on API server!");
}
});
console.log(await result);
I received response in the html format. Part of response:
[0-0] <input class="form-control" data-val="true" data-val-number="The field Maximum days in advance an online reservation can be made must be a number." data-val-range="The field Maximum days in advance an online reservation can be made must be between 1 and 366." data-val-range-max="366" data-val-range-min="1" data-val-required="The Maximum days in advance an online reservation can be made field is required." id="MaxFutureReservationTimeDays" name="MaxFutureReservationTimeDays" onkeypress="return event.charCode === 13 || (event.charCode >= 48 && event.charCode <= 57)" type="number" value="20" />
How can I find some value in this text and store it in a variable? For example value="20" for MaxFutureReservationTimeDays. Thanks.
I tried use
string.includes(word);
2
Answers
You can use
DOMParser
to do that.If the response has multiple input elements and you want to store the values from every input, you can loop over all input elements and store the data in an object.
If you trust the HTML input, then a simple solution would be to put it inside an HTML element, so that the DOM gets updated, and then use the JS DOM manipulation functions to retrieve what you need, considering that you know how the HTML is structured and where the value is stored.
An example from the reference below:
Ref: Extract the text out of HTML string using JavaScript