Checking If A Html Tag Exist On This Scenario Using Js
Solution 1:
1. First question is what can I use to check if this option exist? I know there is a method (myfile.getElementsByTagName("")) but in my case I have a lot of tags that has the tag-name "option".
You can use document.querySelector
to find the first matching element for any valid CSS selector. It returns the element instance if found, or null
if not found. So for instance, from what you've shown of your HTML, you could do this:
if (document.querySelector('select[name="metric-name"] option[value="13"]')) {
// Yes, it's there
} else {
// No, it's not
}
2. If there is a method to check if the option exist, is it [going to] work on this scenario since my option is on the page even though is displayed only when accountId from session is not equal to 5
According to what I can find about cfif
, the option
won't be on the rendered page in the browser if that condition is true. So the above would work.
Example without the option
:
if (document.querySelector('select[name="metric-name"] option[value="13"]')) {
console.log("Yes, it's there");
} else {
console.log("No, it's not there");
}
<selectclass="ue-select-box box-model"name="metric-name"><optionvalue="1"selected>Logins (including from CRM)</option><optionvalue="2">Listened to a Call (including from CRM)</option><optionvalue="15">Set up a New Report/Edited a Report</option></select>
Example with the option
:
if (document.querySelector('select[name="metric-name"] option[value="13"]')) {
console.log("Yes, it's there");
} else {
console.log("No, it's not there");
}
<selectclass="ue-select-box box-model"name="metric-name"><optionvalue="1"selected>Logins (including from CRM)</option><optionvalue="2">Listened to a Call (including from CRM)</option><optionvalue="15">Set up a New Report/Edited a Report</option><optionvalue="13">Staff Activity - Deactivate</option></select>
Post a Comment for "Checking If A Html Tag Exist On This Scenario Using Js"