Just need a little help on something I haven't been able to find the answer to. I have a drop down menu in HTML that is supposed to display a div with an image associated when a drop down selection is made. The problem is that currently, it will load every div when the page opens and will only hide the divs when a selection is made. I would like nothing shown until the actual selection is made if that makes sense. Here is what I have so far (simplified for the problem's sake):
<select onchange="toggle_form_element(this)" name="parts" id="parts">
<option value="-">Please choose an image</option>
<option value="0">img 1</option>
<option value="1">img 2</option>
<option value="2">img 3</option>
</select>
<span id="dropdown">
<div id="img1">image 1.jpg</div>
<div id="img2">image 2.jpg</div>
<div id="img3">image 3.jpg</div>
</span>
<script type="text/javascript">
function toggle_form_element(select) {
var divSelect = select.value;
var elements = dropdown.getElementsByTagName("div");
for (var i = 0; i < elements.length; i++) {
if (i == divSelect) {
elements[i].style.display = "block";
} else {
elements[i].style.display = "none";
}
}
}
</script>
Any help would be greatly appreciated. Thank you!
Add this to your styles
<style>
#dropdown .image
{
display: none;
}
</style>
Then add a class to your elements:
<div id="img1" class="image">image 1.jpg</div>
<div id="img2" class="image">image 2.jpg</div>
<div id="img3" class="image">image 3.jpg</div>
So, at the beginning images will be hidden by CSS.
After making a choice in your select, your JS will make one of them shown.
Related
I'm using select2 for custom select box. But I can't change select option icon. Is it possible to change icon dynamically using jQuery ?
I find icon in Dom ...
<span class="select2-item"><i class="ico-status-pending"></i> <span>Pending</span></span>
My html ..
<div class="aside-head__status-item">
<div class="select2-wrap">
<select class="select2-img" data-width="100%">
<option value="ico-status-pending" selected>Pending</option>
<option value="ico-status-open">Open</option>
<option value="ico-status-resolved">Resolved</option>
<option value="ico-status-close">Closed</option>
</select>
</div>
</div>
Not sure if i understand you correctly
If you want to change dynamically the icon, you could trigger the select2 with the desired icon class:
for example:
$('.select2-img').val("ico-status-resolved").trigger("change")
if you want to use selector directly in the DOM, you could use:
$(".select2-item i").removeClass().addClass("ico-status-open");
$(".select2-item span").text("Open");
of course the solution is different if $(".select2-item") is not uniq..
Not sure if i understand you correctly, do you mean the icon arrow down which is displayed in the right corner of the option menu?
If so, then you can change it like this:
$('.select2-img').select2();
.select2-container--default .select2-selection--single .select2-selection__arrow b {
background-image: url(https://www.iconninja.com/files/664/91/782/small-arrow-down-icon.png);
background-color: transparent;
background-size: contain;
border: none !important;
height: 20px !important;
width: 20px !important;
margin: auto !important;
top: auto !important;
left: auto !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script>
<span class="select2-item"><i class="ico-status-pending"></i> <span>Pending</span></span>
<div class="aside-head__status-item">
<div class="select2-wrap">
<select class="select2-img" data-width="80%">
<option value="ico-status-pending" selected>Pending</option>
<option value="ico-status-open">Open</option>
<option value="ico-status-resolved">Resolved</option>
<option value="ico-status-close">Closed</option>
</select>
</div>
</div>
Rather than hack away at select2's DOM elements, select2 provides an API exactly for your requirements, the templateSelection option.
Your original code doesn't indicate how the icons are displayed/generated, only that the option has a value= that appears to correspond to your icons (the image also shows them in a different order).
To set how the drop down is displayed after you select an option, use this:
$('.select2-img').select2({
templateSelection: function(data, container) {
if (data == null || !container) return null;
return $("<i class='" + data.id + "'></i>");
}
});
where data.id = the option's value=
if you return a jQuery object, then it will be output as-is, otherwise will be output using .text() (and thus remove any html)
I have a div with some content that gets hide after 5 seconds but below it, I have a drop-down with some options. So what happens when the select box is open and at the same time the div gets hidden, the options box remains in the same place and doesn't shift upwards with the select box.
Creating a custom drop-down is not an option. As well as making the above div absolute or fixed. Making the drop-down blur is also not an option. Can someone tell me if this default behaviour can be somehow changed?
The client does not agree for a custom solution.
The client does not want the above div to float or position absolute or fixed. He wants the same setup but this thing fixed.
Closing the select box or using blur() is also not that the client wants.
<!DOCTYPE html>
<html>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<body>
<div class='war'>SOME RANDOM CODE which gets hidden after 5 seconds. Make sure to open the drop down</div>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<script>
setTimeout(function(){ $(".war").hide(); }, 3000);
</script>
</body>
</html>
How about this workaround:
- When the select menu is clicked, it gets a size attribute that is as big as all its containing option elements so that it moves with the element in the window, as it disappears.
- If you add some css styling it does not become apparent to the user that the size of the selection element has changed while he/she is in the process of selecting an option.
- Unfortunately the standard arrow styling is different, depending on the browser. My solution looks best in chrome but not optimal in firefox for example - so further browser specific optimization should be added.
Here is the corresponding code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>example</title>
<style>
#box {
width: 100px;
height: 100px;
border: 1px solid black;
}
#options {
overflow: hidden;
}
#first::after {
content: " ⯆ ";
font-size: 10px;
}
</style>
</head>
<body>
<div id="box"></div>
<select name="selectionarea" id="options" onclick="makeBoxDisappear();expandSelect()" onfocusout="minimizeSelect()">
<option value="option1" id="first">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
<option value="option4">option4</option>
<option value="option5">option5</option>
</select>
<script>
function makeBoxDisappear() {
setTimeout(() => document.getElementById("box").style.display = "none", 1000);
}
function expandSelect() {
document.getElementById("options").size="5";
}
function minimizeSelect() {
document.getElementById("options").size="1";
}
</script>
</body>
</html>
In my code I am using bootstrap css for styling . I have written a code for a drop down menu with class form-control. The problem is that I cant adjust the width of the form. It is now like occupying the total width of the webpage like:
--------------------------------------------------------------------------------------------------------| Choose the option
I just need to adjust the size of the dropdown . I am totally new to this field .
Somebody can help me out .?
I am attaching the part of code with this :
<body>
<select class="form control">
<option value= "1">one</option>
<option value= "2">two</option>
<option value= "3">three</option>
</select>
</body>
bootstrap link
Well, if you are using Bootstrap, the class .form-control by default has the following properties:
.form-control {
position: relative;
z-index: 2;
float: left;
width: 100%;
margin-bottom: 0;
}
So, if you are using bootstrap you will have to override it using a custom class/id, or set a parent with a fixed width.
HTML
<div class="form-container">
<select class="form control">
<option value= "1">one</option>
<option value= "2">two</option>
<option value= "3">three</option>
</select>
</div>
CSS
.form-container{
width: 30%; //or width: 200px, etc
}
You can try using the bootstrap grid system. Adding a pre-defined responsive class like "col-xs-4" to your class list for the select element. (note this will only cover your width requirement, but if all elements use the grid system is can help keep the form looking clean and well organized).
A helpful reference for the bootstrap grid system may be found here http://www.w3schools.com/bootstrap/bootstrap_grid_system.asp
Link your own stylesheet to the document...
Declare the class form-control in this document.. or add a new class to the html next to this, then add the height/width attributes:
Html:
<select class="form-control resizeformc">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
CSS:
.resizeformc {
width: 150px;
height:200px;
}
I am just wondering what's the name of the below form? I was Googling from morning for the list of HTML forms but I couldn't find this kind of form anywhere. Can anyone tell me the exact name of this form and is this available in HTML forms?
I just want to add this kind of form in my website. Is that available for HTML or should I use JavaScript or its only limited for Windows applications?
Here's a little sample to get you started: http://jsfiddle.net/eUDRV/3/
This example will move items (one or multiple) from the left to the right and back again. Whatever item(s) are selected in the right side will update the textbox on the right side.
We are using the elements:
select
input type="button"
input type="text"
Framed by:
div
section
Styled with simple CSS. Functionality is provided with JavaScript.
I'm using the jQuery library to make things a little easier. This could also be done with pure JavaScript.
$("#btnLeft").click(function () {
var selectedItem = $("#rightValues option:selected");
$("#leftValues").append(selectedItem);
});
$("#btnRight").click(function () {
var selectedItem = $("#leftValues option:selected");
$("#rightValues").append(selectedItem);
});
$("#rightValues").change(function () {
var selectedItem = $("#rightValues option:selected");
$("#txtRight").val(selectedItem.text());
});
SELECT, INPUT[type="text"] {
width: 160px;
box-sizing: border-box;
}
SECTION {
padding: 8px;
background-color: #f0f0f0;
overflow: auto;
}
SECTION > DIV {
float: left;
padding: 4px;
}
SECTION > DIV + DIV {
width: 40px;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<section class="container">
<div>
<select id="leftValues" size="5" multiple></select>
</div>
<div>
<input type="button" id="btnLeft" value="<<" />
<input type="button" id="btnRight" value=">>" />
</div>
<div>
<select id="rightValues" size="4" multiple>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<div>
<input type="text" id="txtRight" />
</div>
</div>
</section>
It's most commonly referred to as a listbox multiselect. Here's a lightweight jQuery multiselect library at loudev.com:
I have a asp:DropDownList with fixed width of 150px, but its list items are not enough to fit in 150px width, so it get cut off in IE (works fine in other browsers)
I search about this, but only got solution for html SELECT.
set a css class for your dropdownlist and then set width to that css class
<style>
.myDropDown
{
width : 150px;
}
</style>
<asp:DropDownList CssClass="myDropDown"></asp:DropDownList>
EDIT
so you must remove width attribute from your dropdownload. then it will be resized depending on items size.
<script type='text/javascript'>
function SetWidthToAuto(drpLst) {
drpLst.style.width = 'auto';
}
function ResetWidth(drpLst) {
drpLst.style.width = '150px';
}
</script>
<div style="width:150px;overflow:hidden;">
<select id="drpTechnology" style='width:150px' onchange='ResetWidth(this)' onblur='ResetWidth(this)' onmousedown='SetWidthToAuto(this)'>
<option value="-1">Browse me..</option>
<option value="1">Short Option</option>
<option value="2">Little bigger than short Option</option>
<option value="3">Largest option available with this select box</option> </select> <div>