How to create conditional logic link with drop-down field in html? - html

How to create conditional logic link with drop-down field in html? eg drop down field= item1, item2 and suit button link based on item1=url1 or item2=url2.

Here's a very basic example showing how to redirect the browser to the url selected from a dropdown among a list of options, when the form gets submitted.
This demo adds a submit event listener to the #mainForm when the page has loaded, that will cancel the default behaviour with event.preventDefault() (mdn) and will visit the page at the url defined in the selected option (mdn).
Plus the dropdown itself listens for the change event printing on console the latest url selected and enabling the submit button as soon as a valid option was chosen.
Two ways to listen to events were showed here... both with addEventListener and with the onchange html attribute in the declarative way.
document.addEventListener('DOMContentLoaded', ()=>{
const mainForm = document.getElementById('mainForm');
mainForm.addEventListener('submit', (event)=>{
event.preventDefault();
const url = fetchSelectedUrl();
location.assign(url);
});
});
function fetchSelectedUrl(){
document.querySelector('#mainForm button[type=submit]').removeAttribute('disabled');
const url = document.getElementById('urls').value;
console.log(`Selected url: ${url}`);
return url;
}
label{
margin-right: 1rem;
font-size: 1rem;
}
#urls{
padding: 0.25rem;
}
form{
width: 50%;
}
.row{
display: flex;
margin-bottom: 1rem;
}
.row > *:nth-child(1){
flex: 25%;
}
.row > *:nth-child(2){
flex: 75%;
}
.submit{
text-align: right;
}
button[type=submit]{
padding: 0.25rem 0.75rem;
cursor: pointer;
font-weight: bold;
}
<form id="mainForm">
<div class="row">
<label for="urls">Select a page to visit:</label>
<select id="urls" onchange="fetchSelectedUrl();">
<option value="" disabled selected>Select an option</option>
<option value="https://google.com">Google (com)</option>
<option value="https://google.de/">Google (de)</option>
<option value="https://google.fr">Google (fr)</option>
</select>
</div>
<div class="submit">
<button type="submit" disabled>GO!</button>
</div>
</form>

Related

Option within select mouse hover [duplicate]

Is it possible to change the default background color of a select list option on hover?
HTML:
<select id="select">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
I have tried option:hover { background-color: red; }, but it is of no use. Does anybody know how to do this?
This can be done by implementing an inset box shadow.
eg:
select.decorated option:hover {
box-shadow: 0 0 10px 100px #1882A8 inset;
}
Here, .decorated is a class assigned to the select box.
Hope you got the point.
Select / Option elements are rendered by the OS, not HTML. You cannot change the style for these elements.
This way we can do this with minimal changes :)
option:hover {
background-color: yellow;
}
<select onfocus='this.size=10;' onblur='this.size=0;' onchange='this.size=1; this.blur();'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Implementing an inset box shadow CSS works on Firefox:
select option:checked,
select option:hover {
box-shadow: 0 0 10px 100px #000 inset;
}
Checked option item works in Chrome:
select:focus > option:checked {
background: #000 !important;
}
There is test on https://codepen.io/egle/pen/zzOKLe
For me this is working on
Google Chrome
Version 76.0.3809.100 (Official Build) (64-bit)
Newest article I have found about this issue by Chris Coyier (Oct 28, 2019) https://css-tricks.com/the-current-state-of-styling-selects-in-2019/
The problem is that even JavaScript does not see the option element being hovered. This is just to put emphasis on how it's not going to be solved (any time soon at least) by using just CSS:
window.onmouseover = function(event)
{
console.log(event.target.nodeName);
}
The only way to resolve this issue (besides waiting a millennia for browser vendors to fix bugs, let alone one that afflicts what you're trying to do) is to replace the drop-down menu with your own HTML/XML using JavaScript. This would likely involve the use of replacing the select element with a ul element and the use of a radio input element per li element.
Select / Option elements are rendered by the OS/Client, not HTML.
You cannot change the style for these elements in modern Browser.
On older clients
select option:checked,
select option:hover {
box-shadow: 0 0 10px 100px #000 inset;
}
Checked option item works in older Chrome:
select:focus > option:checked {
background: #000 !important;
}
Adding/toggling size attributes on focus event as suggestest by #Krishnaraj works pretty well on desktop using mouse controls.
However, the previous answers don't work well with keyboard controls.
The following example wraps the aforementioned state toggling into a javaScript helper function and adds additional event listeners for better accessibility
setSelectHover();
function setSelectHover(selector = "select") {
let selects = document.querySelectorAll(selector);
selects.forEach((select) => {
let selectWrap = select.parentNode.closest(".select-wrap");
// wrap select element if not previously wrapped
if (!selectWrap) {
selectWrap = document.createElement("div");
selectWrap.classList.add("select-wrap");
select.parentNode.insertBefore(selectWrap, select);
selectWrap.appendChild(select);
}
// set expanded height according to options
let size = select.querySelectorAll("option").length;
// adjust height on resize
const getSelectHeight = () => {
selectWrap.style.height = "auto";
let selectHeight = select.getBoundingClientRect();
selectWrap.style.height = selectHeight.height + "px";
};
getSelectHeight(select);
window.addEventListener("resize", (e) => {
getSelectHeight(select);
});
/**
* focus and click events will coincide
* adding a delay via setTimeout() enables the handling of
* clicks events after the select is focused
*/
let hasFocus = false;
select.addEventListener("focus", (e) => {
select.setAttribute("size", size);
setTimeout(() => {
hasFocus = true;
}, 150);
});
// close select if already expanded via focus event
select.addEventListener("click", (e) => {
if (hasFocus) {
select.blur();
hasFocus = false;
}
});
// close select if selection was set via keyboard controls
select.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
select.removeAttribute("size");
select.blur();
}
});
// collapse select
select.addEventListener("blur", (e) => {
select.removeAttribute("size");
hasFocus = false;
});
});
}
body {
font-size: 10vmin;
}
select {
--selectHoverCol: #999;
--selectedCol: red;
width: 100%;
font-size: 1em;
padding: 0.3em;
background-color: #fff;
}
select:focus {
border-radius: 0px;
border-color: red;
background: #fff;
outline: none;
}
.select-wrap {
position: relative;
}
.select-wrap:focus-within select {
position: absolute;
top: 0;
left: 0;
z-index: 10
}
option:hover {
background-color: var(--selectHoverCol);
color: #fff;
}
option:checked {
box-shadow: 0 0 1em 100px var(--selectedCol) inset;
}
<select class="selectHovercolor">
<option value="volvo" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<p>paragraph</p>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="Lexus">Lexus</option>
<option value="Mercedes">Mercedes</option>
</select>
<p>paragraph</p>
The select field is wrapped in a <div> with relative position.
:focus-within pseudo state toggles the select positioning between absolute and initial (static) – this way we can avoid layout shifts.
the wraping div height is recalculated on resize
since the focus event coincides with with the click event, we add a delay for click events, triggering the select field to collapse after selection
If an option was selected via keyboard controls and selection was confirmed by pressing "enter" - the size attribute is removed.
size attribute is set according to the actual number of <option> elements
You can do this, just know that it will change all of the select inputs throughout the html, it doesn't change the blue hover, but it does style everything else.
option {
background: #1b1a1a !important;
color: #357b1d !important;
}
select {
background: #1b1a1a !important;
color: #357b1d !important;
}
// If you also want to theme your text inputs:
input {
background: #1b1a1a !important;
color: #357b1d !important;
}
<html>
<head>
<style>
option:hover {
background-color: yellow;
}
</style>
</head>
<body>
<select onfocus='this.size=10;' onblur='this.size=0;' onchange='this.size=1; this.blur();'>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
This worked for me in chrome!
<select onfocus='this.size=10;'>
<option>Crossing</option>
<option>Crossing Up</option>
<option>Crossing Down</option>
</select>
<style>
select option:hover {
box-shadow: 0 0 10px 100px green inset;
color:white;
}
select option:checked{
box-shadow: 0 0 10px 100px green inset;
}
</style>
However, the checked option's background will remain same even if i hover on another option
By the way, you can do that one as well.
Here is the link for that: https://www.w3schools.com/howto/howto_custom_select.asp
I would consider switching from a <select> element to a <div> list, like below:
https://jsfiddle.net/getbutterfly/gquh02dz/
This will make it cross-browser compatible. Every other method using CSS appearance tricks and <select> dropdowns is hacky.
HTML
<div class="sel">
<div class="label">Select option...</div>
<div class="options">
<div>Option 1</div>
<div>Option 2</div>
<div>Option 3</div>
<div>Lot of text to display, so it can expand multiple lines and expand the select main text also</div>
</div>
</div>
JavaScript
const sel = document.querySelector('.sel');
const label = document.querySelector('.label');
const options = document.querySelector('.options');
options.setAttribute('hidden', true);
sel.addEventListener('click', (e) => {
e.stopPropagation();
options.removeAttribute('hidden');
});
document.body.addEventListener('click', (e) => {
options.setAttribute('hidden', true);
});
options.addEventListener('click', (e) => {
if (e.target.tagName === 'DIV') {
e.stopPropagation();
label.textContent = e.target.textContent;
e.target.classList.add('selected');
Array.from(e.target.parentNode.children).forEach((child) => {
if (child !== e.target) {
child.classList.remove('selected');
}
});
options.setAttribute('hidden', true);
}
});
CSS
* {
box-sizing: border-box;
}
.sel {
color: #000000;
width: 250px;
box-sizing: border-box;
background-color: #ffffff;
border: 1px solid #000000;
overflow: hidden;
background: url("data:image/svg+xml,<svg height='10px' width='10px' viewBox='0 0 16 16' fill='%23000000' xmlns='http://www.w3.org/2000/svg'><path d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/></svg>") no-repeat calc(100% - 10px) 14px;
}
.label,
.sel .options div {
padding: 10px;
}
.selected {
background-color: #ff0000;
}
.sel .options {
width: 250px;
background-color: #ffffff;
}
.sel .options div:hover {
background-color: #00ff00;
}
With a bit of extra CSS, the dropdown can be animated and the selected text can be truncated to fit inside a fixed height, behaving exactly like a <select> element.
I realise this is an older question, but I recently came across this need and came up with the following solution using jQuery and CSS:
jQuery('select[name*="lstDestinations"] option').hover(
function() {
jQuery(this).addClass('highlight');
}, function() {
jQuery(this).removeClass('highlight');
}
);
and the css:
.highlight {
background-color:#333;
cursor:pointer;
}
Perhaps this helps someone else.
this is what you need, the child combinator:
select>option:hover
{
color: #1B517E;
cursor: pointer;
}
Try it, works perfect.
Here's the reference: http://www.w3schools.com/css/css_combinators.asp

I'm trying to change the style of a button with typescript

I'm trying to make a timer; just a simple, HTML timer. Now, I'm trying to get it so that the first button (button1) changes the background colour of the button to #0000FF if var button1 (the variable that determines what the button says) is "Pause" and to #00FF00 if var button1 is anything else. Here's my HTML (note: I am using angular so it might look a little weird):
<!DOCTYPE html>
<html>
<div class="screen">
<div class="timer">
00:00:00
</div>
<div class="buttons">
<button class="button1" id="buuton" onclick="timeout(40, 1)">
{{button1}}
</button>
<button class="button2">
{{button2}}
</button>
</div>
</div>
</html>
Here's my typescript (again might look a little weird):
timeLeft: number //Time left in milliseconds
currTime: number //Time on stopwatch in milliseconds
button1: string //Button 1
button2: string //Button 2
timeout (ms: number, func: number) {
setTimeout(() => {
if (func == 0) {
null
} else if (func == 1) {
if (this.button1 == "Pause") {
document.getElementById("buuton").style.backgroundColor = "#0000ff"
} else {
document.getElementById("buuton").style.backgroundColor = "#00ff00"
}
}
}, ms);
}
And here's my CSS:
.timer {
height: 3.5em;
line-height: 3em;
display: block;
margin-top: 2em;
margin-bottom: 1em;
margin-left: 0.33em;
font-weight: bolder;
}
.buttons {
height: 1em;
line-height: 1.5em;
margin-top: 0.1vh;
margin-bottom: 2em;
margin-left: 0.33em;
}
.screen {
position: fixed;
height: 93.5vh;
width: 99vw;
left: 0.5vw;
top: 0.5vh;
}
I don't know what I'm doing wrong. Can you please help me?
Edit 1 (16 Jun 2020 10:49:39) - No, there were no error messages.
Just as #CodeMonkey mentioned, change your onclick event listener to be (click) instead. When event binding in Angular, we hook an event listener to get notified whenever an event is fired. Angular docs
Since your using Angular, updating the click event listener for your <button> to (click)="timeout(40, 1)" fixes the issue. Wrapping the event listener click inside parentheses indicates that the component property is tied up with the click event.
<!DOCTYPE html>
<html>
<div class="screen">
<div class="timer">
00:00:00
</div>
<div class="buttons">
<button class="button1" id="buuton" (click)="timeout(40, 1)">
{{button1}}
</button>
<button class="button2">
{{button2}}
</button>
</div>
</div>

How make select2 readonly?

I know that "readonly" feature does not exist for select2. Please check here.
How do I achieve that?
Any help would be appreciated.
Thanks.
Note: I cant use disabled. If I use disabled, I will not get the list of selected value.
I guess that issue is resolved or may be available for higher version as this worked for me.
Please go through the example to make a select2 readonly here: Select 2
In js code:
$("select").select2("readonly", true);
You can put your own CSS like:
.select2-container.select2-container-disabled .select2-choice {
background-color: #ddd;
border-color: #a8a8a8;
}
$('.js-example-basic-single').select2({
disabled: true
});
Render a disabled attribute after the page loads resolved for me.
A reminder, a field with the disabled attribute is not sent on forms
as the question says: How to make select2 readonly?, and as the user pointed out: the function "readonly" does not exist for select2. According to the select2 team developers: read this.
I just want to add a couple of things:
disabled is not the same as readonly! be careful.
$(element).select2({ disabled: true }); only works on versions prior to V4. In fact the answer marked as correct refers to V3.
Having said that, I want to share a simple suggestion:
destroy the element and make it readonly. $(element).select2('destroy').attr("readonly", true)
if you need it available again you can always call him again. $(element).select2()
tip:
if you want it to look like the previous element, just remove the default css style: $(element).select2('destroy').attr("readonly", true).css({'-moz-appearance': 'none','-webkit-appearance': 'none'});
Try this:
$('select option:selected').attr('disabled','disabled');
EDIT:
For those using Select 2 version 4+, the new way to do it should be:
$("select").prop("disabled", true); // instead of $("select").enable(false);
After clarifying the question, this is the right way to do it:
$('select').select2().enable(false);
After few tests trying to block expanding/opening of the Select2 items, I've found the way to apply a listener on each natives select tags having Select2 attribute id... and detect on opening event if the native tag is readonly.
$('select[data-select2-id]').on('select2:opening', function (e) {
if( $(this).attr('readonly') == 'readonly') { // Check if select tag is readonly.
console.log( 'readonly, can't be open !' );
e.preventDefault();
$(this).select2('close');
return false;
}else{
console.log( 'expandable/selectable' );
}
});
For more custom on the Select2, we can add some CSS ...
select[readonly] ~ .select2.select2-container .selection [role="combobox"] {
background: repeating-linear-gradient(
45deg
, #b4d2e4, #b4d2e4 10px, rgba(255, 255, 255, 0.15) 10px, rgba(255, 255, 255, 0.15) 20px) !important;
box-shadow: inset 0 0 0px 1px rgba
jQuery(document).ready(function($) {
// Implement Select2
$( 'select' ).select2({
placeholder: "Saisissez un mot pour aide à saisie",
tags: true, // allow create new
width: '100%'
});
// Just extra button to swap readonly
$('#button_demo_2').off().on( 'click',function(e){
console.log( $('#select_demo_2').attr('readonly') );
if( typeof( $('#select_demo_2').attr('readonly') ) == 'undefined' ){
$('#select_demo_2').attr('readonly','readonly');
}else{
$('#select_demo_2').removeAttr('readonly');
}
} );
// Demo code...
$('select[data-select2-id]').on('select2:opening', function (e) {
if( $(this).attr('readonly') == 'readonly') {
console.log( 'can not open : readonly' );
e.preventDefault();
$(this).select2('close');
return false;
}else{
console.log( 'can be open : free' );
}
});
});
*{
margin : 0;
padding : 0;
}
body {
height: 100vh;
background-color: #215a82;
font-family: 'Roboto',sans-serif;
background: linear-gradient(180deg,#215a82 0%,#152135 100%) no-repeat;
display: -webkit-box !important;
display: -ms-flexbox !important;
display: flex !important;
-webkit-box-align: center !important;
-ms-flex-align: center !important;
align-items: center !important;
-ms-flex-pack: distribute !important;
justify-content: space-around !important;
}
.container {
display: -webkit-box !important;
display: -ms-flexbox !important;
display: flex !important;
}
div[role="box"]{
padding:1rem;
margin:2rem
display: block;
}
pre {
line-height: 1rem;
height: 1.5rem;
color: white;
}
select[readonly] ~ .select2.select2-container .selection [role="combobox"] {
background: repeating-linear-gradient(45deg, #dadada, #dadada 10px, rgba(255, 255, 255, 0.66) 10px, rgba(255, 255, 255, 0.66) 20px) !important;
box-shadow: inset 0 0 0px 1px #77859133;
}
input{
display: block;
padding: 0.5rem;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script>
<main class="container">
<div role="box">
<pre></pre>
<select class="form-control inputFocusable" id="select_base" name="select_base" tabindex="-1" aria-hidden="true">
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
<div role="box">
<pre>readonly</pre>
<select class="form-control inputFocusable" id="select_demo_1" name="select_demo_1" tabindex="-1" aria-hidden="true" readonly>
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
<div role="box">
<pre>readonly="readonly"</pre>
<select class="form-control inputFocusable" id="select_demo_2" name="select_demo_2" tabindex="-1" aria-hidden="true" readonly="readonly">
<option value="A">Item A</option>
<option value="B">Item B</option>
<option value="C">Item C</option>
</select>
</div>
</main>
<div role="box">
<pre>readonly ?</pre>
<input id="button_demo_2" type="button" value="Fix / Remove">
</div>
You can use
$("#id_example").select2({readonly:true});
Disabled not send data on POST action.
you cannot make the select2 as readonly as per new version of select2,
follow the link for the css hack, it works like a charm
https://stackoverflow.com/a/55001516/9945426
$('#element_id').select2().readonly(true);
This can be define your element readonly and still send your POST data
Try this :
$(".example_element").css("pointer-events","none");

How to Keep Open Kendo DropdownList on Item selection

I have KendoDropdownlist in web application
#(Html.Kendo().DropDownList()
.HtmlAttributes(new { #style = "width: 200px;" })
.Name("cbStatusFilter" + i)
.DataTextField("Name")
.DataValueField("Id")
.TemplateId("tpStatusFilter")
.BindTo((System.Collections.Generic.IEnumerable<dynamic>)ViewData["statuses"])
.Events(c => c.Select("cbFilter_Select").Close("cbFilter_Close"))
)
it has templete as following
<script type="text/x-kendo-template" id="tpStatusFilter">
<input type="checkbox" id="#= Name #" name="#= Name #" value="#= Id #" class="check-input-status" #=selected ? "checked" :"" # />
<label for="#= Name #">#= Name #</label>
</script>
when use select item from dropdown i dropdown list gets close. but i want to keep it open and want to close when i click on some-other controls
How can i achieve it ?
Thanks
It can be done, but it based on knowledge of how JavaScript for the drop down is written. If Kendo re-writes the JavaScript (doubtful at this point in time), this answer may not work.
If you look into the source code for the function, you will see that in the onChange event, the the Kendo drop-down code will toggle the visibility of the drop down items. Knowing this, you can "trick" the code into doing what you want by adding a call to jQuery's .hide()function. See the call tocolor.hide()` in the below snippet. Now, when the Kendo control onChange event fires, toggling the visibility will now show the list.
The control will automatically close the list when it looses focus, so you don't have to worry about that.
var color = $("#color").data("kendoDropDownList");
function onChange() {
var value = $("#color").val();
color.hide();
$("#cap")
This code is based on http://demos.telerik.com/kendo-ui/dropdownlist/index and has only been tested in Chrome.
$(document).ready(function() {
debugger;
var data = [{
text: "Black",
value: "1"
}, {
text: "Orange",
value: "2"
}, {
text: "Grey",
value: "3"
}];
// create DropDownList from input HTML element
var color = $("#color").kendoDropDownList({
dataTextField: "text",
dataValueField: "value",
dataSource: data,
index: 0,
change: onChange
});
// create DropDownList from select HTML element
$("#size").kendoDropDownList();
// IMPORTANT
// save pointer to actual kendo control.
// This is required to make the call to the .hide()
// function work correctly.
var color = $("#color").data("kendoDropDownList");
color.select(0);
var size = $("#size").data("kendoDropDownList");
function onChange() {
var value = $("#color").val();
color.hide();
$("#cap")
.toggleClass("black-cap", value == 1)
.toggleClass("orange-cap", value == 2)
.toggleClass("grey-cap", value == 3);
};
$("#get").click(function() {
alert('Thank you! Your Choice is:\n\nColor ID: ' + color.value() + ' and Size: ' + size.value());
});
});
<html>
<head>
<base href="http://demos.telerik.com/kendo-ui/dropdownlist/index">
<style>
html {
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
}
</style>
<title></title>
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.dataviz.default.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1119/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example">
<div id="cap-view" class="demo-section k-header">
<h2>Customize your Kendo Cap</h2>
<div id="cap" class="black-cap"></div>
<div id="options">
<h3>Cap Color</h3>
<input id="color" value="1" />
<h3>Cap Size</h3>
<select id="size">
<option>S - 6 3/4"</option>
<option>M - 7 1/4"</option>
<option>L - 7 1/8"</option>
<option>XL - 7 5/8"</option>
</select>
<button class="k-button" id="get">Customize</button>
</div>
</div>
<style scoped>
.demo-section {
width: 460px;
height: 300px;
}
.demo-section h2 {
text-transform: uppercase;
font-size: 1em;
margin-bottom: 30px;
}
#cap {
float: left;
width: 242px;
height: 225px;
margin: 20px 30px 30px 0;
background-image: url('../content/web/dropdownlist/cap.png');
background-repeat: no-repeat;
background-color: transparent;
}
.black-cap {
background-position: 0 0;
}
.grey-cap {
background-position: 0 -225px;
}
.orange-cap {
background-position: 0 -450px;
}
#options {
padding: 1px 0 30px 30px;
}
#options h3 {
font-size: 1em;
font-weight: bold;
margin: 25px 0 8px 0;
}
#get {
margin-top: 25px;
}
</style>
</div>
</body>
</html>
you can also use the event counts::
I'm using kendo slightly different so you may need to modify it to fit the way you're using it.
<input id="dropdown" k-keepOpen/>
Then for the work::
$("#dropdown").kendoDropDownList({
keepOpen:$("#dropdown")[0].hasAttribute("k-keepOpen"),//this will turn it on and off by your element
preventCloseCnt:0,//this is used for managing the event
select:function(e){ this.preventCloseCnt=2;},//set the counter to bypass closing
close:function(e){
if(this.keepOpen && this.preventCloseCnt >0 ){//now the next time something tries to close it will work ( unless it's a change )
e.preventDefault();
this.preventCloseCnt--;
}
}
});

How to place a drop-down and a div inside a div, so that it ll act as a dropdown?

I am really confuded in this. I have a label and select drop-down inside my container which is right aligned.
GOAL
Container should act like a drop-down. Only sort-by label should be displayed initially.When user clicks on it, it should shoe the option to the user.
Problem
I don't know how to trigger drop down when i click on the sort by label.
<div class="container">
<label class="Sortlabel">Sort by</label>
<select>
<option>2</option>
<option>22</option>
<option>222</option>
</select>
</div>
If i must use jquery or JS, i ll add these tags also. Any suggestions??
And what is the difference with this one : http://jsfiddle.net/csdtesting/vuc81u87/
Result is the same.
<div class="container">
<label class="Sortlabel"></label>
<select>
<option>Sort by</option>
<option>22</option>
<option>222</option>
</select>
</div>
select
{
width:100%;
-webkit-appearance: none;
}
.container
{
float: right;
width:190px;
}
But if you insists.I took this idea
and here it is (Pure Javascript): http://jsfiddle.net/csdtesting/ybjdsqrx/
var state = false;
// <select> element displays its options on mousedown, not click.
showDropdown = function(element) {
var event;
event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown', true, true, window);
element.dispatchEvent(event);
};
// This isn't magic.
window.runThis = function() {
var dropdown = document.getElementById('sel');
showDropdown(dropdown);
};
select {
-webkit-appearance: none;
border: none;
width: 70%;
}
.container {
width: 100%;
float: left;
width: 190px;
border: 1px solid;
}
.Sortlabel {
width: 20%;
}
}
<div class="container">
<label class="Sortlabel" onclick="runThis()">Sort by</label>
<select id="sel">
<option></option>
<option>2</option>
<option>22</option>
<option>222</option>
</select>
</div>