I am trying to retain the value (URL) of selectbox1 when filtering what is shown in selectbox1 by selecting a value in selectbox0 . It is being filtered against selectbox2 .
The issue I am having is that it is not the URL (value) is not retained, only the inner text is kept in selectbox1 after I filter it.
Another thing that I would like to solve is that I would like to make selectbox2 hidden from the user, if possible.
<html>
<head>
<title>Links</title>
<select id="selectbox0" name="" ;>
<option value="" selected>All</option>
<option value="Google">Google</option>
<option value="Microsoft">Microsoft</option>
<option value="Apple">Apple</option>
</select>
<select id="selectbox1" name="" ;>
<option value="https://www.google.com" selected>Google</option>
<option value="https://www.microsoft.com">Microsoft</option>
<option value="https://www.apple.com">Apple</option>
</select>
<select id="selectbox2" name="" ;>
<option value="https://www.google.com" selected>Google</option>
<option value="https://www.microsoft.com">Microsoft</option>
<option value="https://www.apple.com">Apple</option>
</select>
<button onclick="openInTabSelect();">Open</button>
<script type="text/javascript">
function openInTabSelect() {
var pSelect = document.getElementById('selectbox1').value;
//console.log(pSelect)
var newTab = safari.self.browserWindow.openTab();
newTab.url = pSelect;
}
var selectbox0 = document.getElementById('selectbox0'),
selectbox1 = document.getElementById('selectbox1'),
selectbox2 = document.getElementById('selectbox2');
selectbox0.addEventListener('change', function() {
selectbox1.innerHTML = '';
for (var childIndex = 0; childIndex < selectbox2.children.length; childIndex++) {
var child = selectbox2.children[childIndex];
if (child.innerHTML.search(selectbox0.value) != -1) {
option = document.createElement('option');
option.innerHTML = child.innerHTML;
selectbox1.appendChild(option);
}
}
});
</script>
</head>
</html>
Expected: Selecting Google in selectbox0 should filter out and only show Google in selectbox1 and retain its value www.google.com which will be used to open in a new tab by clicking on button Open .
Exp. element: < option value="https://www.google.com" selected="">Google< /option >
Actual: Selecting Google in selectbox0 does filter out and only show Google in selectbox1 but does not retain its value www.google.com .
Act. element: < option >Google< /option >
I believe I found your problem. You are using innerHTML
to get a value, but you want the value. innerHTML
gets the text in your option field, not the value. Try this:
if (child.search(selectbox1.value) != -1) {
option = document.createElement('option');
option.innerHTML = child.innerHTML;
selectbox1.appendChild(option);
}
Your .getElementById()
statement points to an id that does not exist selectbox
:
var pSelect = document.getElementById('selectbox').value;
I'm not quite sure which id you were intending to point to, but try clearing this up first.
Thanks to all for reading this, but I managed to figure it out myself. I am sorry for asking the question a bit prematurely. By adding a second line where I defined the value of option first it worked.
option.value = child.value;
option.innerHTML = child.innerHTML;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.