Brilliant
Staff member
- Joined
- Dec 31, 2024
- Messages
- 378
- Reaction score
- 7
- Points
- 18
- User icon
- <svg xmlns="http://www.w3.org/2000/svg" height="14" width="15.75" viewBox="0 0 576 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2025 Fonticons, Inc.--><path fill="#63E6BE" d="M309 106c11.4-7 19-19.7 19-34c0-22.1-17.9-40-40-40s-40 17.9-40 40c0 14.4 7.6 27 19 34L209.7 220.6c-9.1 18.2-32.7 23.4-48.6 10.7L72 160c5-6.7 8-15 8-24c0-22.1-17.9-40-40-40S0 113.9 0 136s17.9 40 40 40c.2 0 .5 0 .7 0L86.4 427.4c5.5 30.4 32 52.6 63 52.6l277.2 0c30.9 0 57.4-22.1 63-52.6L535.3 176c.2 0 .5 0 .7 0c22.1 0 40-17.9 40-40s-17.9-40-40-40s-40 17.9-40 40c0 9 3 17.3 8 24l-89.1 71.3c-15.9 12.7-39.5 7.5-48.6-10.7L309 106z"/></svg>
To create a bookmarklet or script that selects <option> elements containing a particular word in a dropdown (<select>), you can use JavaScript. Here’s how to do it:
---
1. Bookmarklet Approach
Code:
How It Works:
1. Prompts the user for a keyword.
2. Iterates through all <select> elements on the page.
3. Finds the first <option> where the text contains the keyword (case-insensitive).
4. Selects the matching option if found.
How to Use:
1. Copy the code above.
2. Create a new bookmark in your browser.
3. Paste the code into the "URL" field of the bookmark.
4. Save the bookmark.
5. Open a webpage with a <select> dropdown, click the bookmark, and enter a keyword.
---
2. Using Console Script
If you don't want to use a bookmarklet, you can run the script directly in your browser's developer console:
Code:
How It Works:
1. Replaces the keyword variable ("example") with the word you’re searching for.
2. Finds all <select> elements and searches their <option> tags for matches.
3. Selects the option if a match is found.
---
3. Enhanced Bookmarklet with Highlighting
If you'd like to highlight the matching options instead of selecting them, use this:
Code:
4. Adding to Your Website
If you want to embed this functionality into a webpage, include the following script:
Customizations
1. Modify prompt() to automatically target specific <select> elements (e.g., by id or class).
2. Adjust the behavior to either select, highlight, or display the matching options in a pop-up.
---
1. Bookmarklet Approach
Code:
JavaScript:
javascript:(function() {
const keyword = prompt("Enter the word to search for:"); // Ask for the keyword
const selectElements = document.querySelectorAll("select"); // Find all <select> elements
selectElements.forEach(select => {
const options = Array.from(select.options);
const matchingOption = options.find(option => option.text.toLowerCase().includes(keyword.toLowerCase()));
if (matchingOption) {
select.value = matchingOption.value; // Select the matching option
}
});
alert(matchingOption ? `Option with "${keyword}" selected.` : `No option found with "${keyword}".`);
})();
How It Works:
1. Prompts the user for a keyword.
2. Iterates through all <select> elements on the page.
3. Finds the first <option> where the text contains the keyword (case-insensitive).
4. Selects the matching option if found.
How to Use:
1. Copy the code above.
2. Create a new bookmark in your browser.
3. Paste the code into the "URL" field of the bookmark.
4. Save the bookmark.
5. Open a webpage with a <select> dropdown, click the bookmark, and enter a keyword.
---
2. Using Console Script
If you don't want to use a bookmarklet, you can run the script directly in your browser's developer console:
Code:
JavaScript:
const keyword = "example"; // Replace with the word you want to search for
const selectElements = document.querySelectorAll("select"); // Find all <select> elements
selectElements.forEach(select => {
const options = Array.from(select.options);
const matchingOption = options.find(option => option.text.toLowerCase().includes(keyword.toLowerCase()));
if (matchingOption) {
select.value = matchingOption.value; // Select the matching option
console.log(`Selected: ${matchingOption.text}`);
} else {
console.log(`No match found in this select element.`);
}
});
How It Works:
1. Replaces the keyword variable ("example") with the word you’re searching for.
2. Finds all <select> elements and searches their <option> tags for matches.
3. Selects the option if a match is found.
---
3. Enhanced Bookmarklet with Highlighting
If you'd like to highlight the matching options instead of selecting them, use this:
Code:
JavaScript:
javascript:(function() {
const keyword = prompt("Enter the word to search for:"); // Ask for the keyword
const selectElements = document.querySelectorAll("select"); // Find all <select> elements
selectElements.forEach(select => {
const options = Array.from(select.options);
options.forEach(option => {
if (option.text.toLowerCase().includes(keyword.toLowerCase())) {
option.style.backgroundColor = "yellow"; // Highlight matching options
} else {
option.style.backgroundColor = ""; // Reset others
}
});
});
alert(`Highlighted options containing "${keyword}".`);
})();
---
4. Adding to Your Website
If you want to embed this functionality into a webpage, include the following script:
JavaScript:
<script>
function highlightOptions(keyword) {
const selectElements = document.querySelectorAll("select");
selectElements.forEach(select => {
const options = Array.from(select.options);
options.forEach(option => {
if (option.text.toLowerCase().includes(keyword.toLowerCase())) {
option.style.backgroundColor = "yellow";
} else {
option.style.backgroundColor = "";
}
});
});
}
// Example usage:
const keyword = prompt("Enter the word to search for:");
highlightOptions(keyword);
</script>
---
Customizations
1. Modify prompt() to automatically target specific <select> elements (e.g., by id or class).
2. Adjust the behavior to either select, highlight, or display the matching options in a pop-up.