React Router Link in
there I’m trying to filter some date using React Router Link in select box with 4 options in it. When I wrap the options with a Router Link I don’t see values in the select box (is empty), any idea’s?, Why can you just use a normal select and option HTML. Then on the onChange event get the selected value then push the new history to your router. The issue is here is using a library with the wrong api. – Neil Jan 8 ’20 at 0:25 , How did the UN (or at least the member states which were involved in this decision) motivate the rejection of the Gibraltar referenda?
I found the solution to my problem. I use useHistory hook from React Router . The functional code for my problem looks like this.
export default function LocationFilter() < let history = useHistory(); function handleChange(value) < history.push(`/?location=$`); > return ( handleChange(event.target.value)>> ); >
- Answer by Margot Cross
- Answer by Meilani Henry
- Answer by Araceli Meadows
- Answer by Ezra Mosley
- Answer by Bishop Moon
- Select option с ссылками
- JS
- Пример:
- JQuery
- Пример:
- C использованием атрибута data
- Пример:
- Имитация селекта
- HTML-разметка:
- CSS:
- JQuery:
- How to Add Links in Select Dropdown Options with JavaScript?
- Add Links in Select Dropdown Options with JavaScript
- Conclusion
- : The HTML Option element
- Styling with CSS
- Examples
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
Answer by Margot Cross
Attach an onChange event to the select and in it push to the history object that the router passes as a prop.
Attach an onChange event to the select and in it push to the history object that the router passes as a prop.
Answer by Meilani Henry
Answer by Araceli Meadows
Answer by Ezra Mosley
Hold selection in state, and have the click handler send a react-router.push action,Hold selection in URL by using in the dropdown,Option 2 makes more sense to me, because it’s declarative and avoids a bunch of pitfalls, so I’m using the template prop to render like so:
const renderLink = () => ( > ) . value =* taken from URL */> />
Answer by Bishop Moon
The primary way to allow users to navigate around your application. will render a fully accessible anchor tag with the proper href.,State to persist to the location.,state: State to persist to the location.
`> activeClassName="active"> // becomes one of these depending on your History and if the route is // active Michael // change the activeClassName `> activeClassName="current"> // change style when link is active > activeStyle=>>Users
Select option с ссылками
По спецификации HTML в нельзя вставить ссылку, если все-таки её туда вставить, то работать она не будет. В таких случаях можно сделать перенаправление браузера по клику на JS или имитацию селекта другими элементами.
JS
Пример:
JQuery
Пример:
C использованием атрибута data
Пример:
Имитация селекта
В целях SEO-перелинковки, в коде страницы должны находится ссылки , в таком случаи можно имитировать другими элементами.
HTML-разметка:
CSS:
.selectlink < position: relative; display: inline-block; width: 200px; text-align: left; margin: 10px; >.selectlink-control < position: relative; padding: 10px 15px; border: 1px solid #dcdfe6; border-radius: 4px; background: #fff; cursor: pointer; >.selectlink-control:after < content: ''; border: 5px solid transparent; border-top: 5px solid #999; position: absolute; top: 50%; right: 15px; margin-top: -3px; >.selectlink ul < display: none; box-sizing: border-box; margin: 0; padding: 0; width: 100%; height: auto; max-height: 200px; position: absolute; top: 100%; left: 0px; border: 1px solid #dadada; border-top: none; background: #fff; z-index: 9999; overflow: auto; user-select: none; >.selectlink li < display: block; margin: 0; padding: 0; >.selectlink li a < position: relative; display: block; margin: 0; padding: 4px 6px 4px 15px; font-weight: normal; font-size: 1em; line-height: 1.1; cursor: pointer; text-decoration: none; color: #333; >.selectlink li a:hover < background: #03a2ff; color: #fff; >@media screen and (max-width: 700px) < .body_pointer < cursor: pointer; >>
Т.к. на мобильных устройствах событие «click» срабатывает только у элементов со свойством cursor: pointer , в коде реализована возможность закрыть выпадающее меню по клику вне области селекта (добавлением класса «body_pointer» к
).JQuery:
$(function() < $(".selectlink-control").click(function()< var $menu_popup = $(this).next(); $menu_popup.slideToggle(200, function()< $('.selectlink ul').not($menu_popup).slideUp(200); if ($menu_popup.is(':hidden')) < $('body').removeClass('body_pointer'); >else < $('body').addClass('body_pointer'); >>); return false; >); $(document).on('click', function(e) < if (!$(e.target).closest('.selectlink').length)< $('body').removeClass('body_pointer'); $('.selectlink ul').slideUp(200); >>); >);
How to Add Links in Select Dropdown Options with JavaScript?
Sometimes, we want to add links in select dropdown options with JavaScript.
In this article, we’ll look at how to add links in select dropdown options with JavaScript.
Add Links in Select Dropdown Options with JavaScript
To add links in select dropdown options with JavaScript, we can add the change listen to the drop down.
And when a option is selected, we redirect to the URL given by the selected option value.
to add the select drop down.
const select = document.querySelector("select") select.onchange = () => < if (select.selectedIndex !== 0) < window.location.href = select.value; >>;
We select the select element with document.querySelector .
Then we set the onchange property to a function that checks if selectedIndex isn’t 0.
If it isn’t, then we get the value and set that as the value of window.location.href to redirect to the selected option.
Now when we select a choice with a URL as its value, we should see the redirect happen.
Conclusion
To add links in select dropdown options with JavaScript, we can add the change listen to the drop down.
And when a option is selected, we redirect to the URL given by the selected option value.
: The HTML Option element
If this Boolean attribute is set, this option is not checkable. Often browsers grey out such control and it won’t receive any browsing event, like mouse clicks or focus-related ones. If this attribute is not set, the element can still be disabled if one of its ancestors is a disabled element.
This attribute is text for the label indicating the meaning of the option. If the label attribute isn’t defined, its value is that of the element text content.
If present, this Boolean attribute indicates that the option is initially selected. If the element is the descendant of a element whose multiple attribute is not set, only one single of this element may have the selected attribute.
The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.
Styling with CSS
Styling the element is highly limited. Options don’t inherit the font set on the parent. In Firefox, only color and background-color can be set, however in Chrome and Safari it’s not possible to set any properties. You can find more details about styling in our guide to advanced form styling.
Examples
Technical summary
Content categories | None. |
---|---|
Permitted content | Text, possibly with escaped characters (like é ). |
Tag omission | The start tag is mandatory. The end tag is optional if this element is immediately followed by another element or an , or if the parent element has no more content. |
Permitted parents | A , an or a element. |
Implicit ARIA role | option |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLOptionElement |
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 21, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.