Cant make the first <option> invisible - css

I have a <select> and want the first option to be invisible since there is not enough space for the text on the mobile version and it gets chopped off anyway so I resolved to just make it disappear but it wont work. Only with the rest of the options (on PC browsers) but not for the first one. Why is that?
This is what I've already tried
CSS:
option:first-child {
color: transparent;
}
HTML:
<select>
<option value="">Please select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Thank you.

option:first-child{
display: none;
}
<select>
<option disabled value>Please select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
using javascript (best way)
(function(){
document.getElementById("first").text = "";
})()
<select id="mySelect">
<option value id="first">Please select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
if you want to hide first option only in mobile, then follow this
(function(){
var window_width = $(window).width();
if(window_width < 480){
document.getElementById("first").text = "";
}
})()
<select id="mySelect">
<option value id="first">Please select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<p>This will hide only in mobile screen</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

You can set it to display: none; only issue is, it won't work in Internet Explorer (surprise!)
Or just leave the <option> empty...

I think you don't really need to add any css for this reason. If you need to the first option to be blank, then remove Please Select .i.e.,
<select>
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If you want some spacing will be there inside the box even though text is removed you can just add a few times to make look the panel wide enough as show below.
<select>
<option value=""> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

Here is a working pluncker for you. Change it to display:none and add select to the CSS
OR
You can use visibility: hidden; instead of display:none in CSS
select option:first-child {
display:none;
}
<select>
<option value="">Please select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

jQuery.fn.toggleOption = function(show) {
jQuery(this).toggle(show);
if (show) {
if (jQuery(this).parent('span.toggleOption').length)
jQuery(this).unwrap();
} else {
if (jQuery(this).parent('span.toggleOption').length == 0)
jQuery(this).wrap('<span class="toggleOption" style="display:none;" />');
}
};

You can use media query to handle small devices and phones:
/* you can set any width you want */
#media (max-width: 400px) {
select option:first-child {
display: none;
}
}

<select>
<option style="display:none;">Please select</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
Thanks... :)

Related

Using bootstrap how to change colour of value button

i am trying to chaneg the colour of text using twitter-bootstrap but something is wrong it is not working.
here is my code:
<select name="number" '<?=htmlspecialchars($s['number'])?>'td>"
<option><?=htmlspecialchars($s['number'])?></option>
<option class="text-cuccess">Tak</option>
<option class="text-danger">Nie</option>
</select>
.newcolor {
color:red;
}
.newcolor1{
color:green;
}
<select id="select" onchange="this.className=this.options[this.selectedIndex].className">
<option style="color:gray" value="null">select one option</option>
<option value="1" class="newcolor">one</option>
<option value="2" class="newcolor">two</option>
<option value="2" class="newcolor1">two</option>
</select>

Materialize.css selectbox rendering duplicate select boxes

I am using Materialize.css library (v 1.0.0), as of 2018 for my project to add the material components. However, I failed to initialize two select boxes with it.
I have two select boxes in my page.
$(function() {
$("#numPagesPaginate").formSelect();
$("#numRatingsEdit").formSelect();
});
<select id="numPagesPaginate" name="numPagesPaginate" data-ng-model="bkCtrl.page.pageSize">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="numRatingsEdit" name="numRatingsEdit" style="display:none">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
However, when I initialize both the select boxes using this only the first one works.
In addition, if I initialize the second selectbox in the script tag elsewhere, the box gets initialized and works, but there is one duplicate selectbox.
Edited: To add this question, I want to tell you that I have read the documentation and the default method. However, no results.
You should follow documentation and initialize globally with one line:
$('select').formSelect();
Just like Serg said, you should follow the documentation properly.
<div class="container">
<div class="row">
<div class="col s6">
<label>Num Pages Paginate</label>
<select id="numPagesPaginate" name="numPagesPaginate" data-ng-model="bkCtrl.page.pageSize">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="col s6">
<label>Num Ratings Edit</label>
<select id="numRatingsEdit" name="numRatingsEdit">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('select').formSelect();
});
</script>

Overflow hidden not working in my firefox

Overflow hidden not working in my firefox rest of the browser working fine
select {
overflow: hidden;
}
<select multiple="" size="20">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
Firefox Output
Other browser Output
The overflow property is used when you want to control the element behavior if the content is bigger than the element size.
In your case, the container is bigger than the content so the content is not overflowing.
Perhaps what you want to do is adjust the size of the select to match the content, if that is the case you have to set the size of the select to match the number of options:
select {
overflow: hidden;
}
<select multiple="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

How to hide <select>

<select class='ui selection dropdown' name='YearLevel' style="display:none" id='YearLevel'>
<option value=''>Year Level</option>
<option value='II'>I</option>
<option value='II'>II</option>
<option value='III'>III</option>
<option value='IV'>IV</option>
</select>
<style>
select#YearLevel{
display:none !important;
}
</style>
I tried everything I know but it just wouldn't work.

How to make Datalist arrow to be always visible

I am trying to have datalist list element always visible. As standard after focused lost, the arrow disappears.
I would like to have it always like this:
Here is the plunker: https://plnkr.co/edit/?p=preview
<input list="browsers" name="myBrowser" />
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
</datalist>
ENVIRONMENT: Angular directive
Any ideas how to achieve it?
Best,
I have got the arrow always visible using css:
input::-webkit-calendar-picker-indicator {
opacity: 100;
}
<input list="browsers" name="myBrowser" />
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
</datalist>

Resources