set dropdown list option with ellipses - javascript

I want to add ellipsis in the dropdown list options if I click the ellipsis able to see the full option values
for example, dropdown list looking like this
<select id ="id">
<option>One - A long text need to be cut off with ellipsis</option>
<option>Two - A long text need to be cut off with ellipsis</option>
</select>
I am expecting the output like as follows
One - A long option....
Two - A long option....
when clicking the ellipses able to see the full option values like
One - A long text need to be cut off with ellipsis
Please see the code which I done so far
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style>
select {
width:200px;
}
</style>
<script>
$(document).ready(function() {
var maxLength = 20;
$('#example > option').text(function(i, text) {
if (text.length > maxLength) {
return text.substr(0, maxLength) + '...';
}
});
});
</script>
</head>
<body>
<select name="example" id="example">
<option value="">John Smith,1019 Your Company PO Box 7169 Poole BH15 9EL</option>
<option value="">91 Western Road,Brighton PO Box 7169 East Sussex England BN1 2NWL</option>
<option value="">John Smith,1019 Your Company PO Box 7169 Poole BH15 9EL</option>
</select>
</body>
</html>
Please help me to get the expected result as above

Here is my solution for you problem.
Its a workaround with the target to ellipse the select value and show the hole text when the options are shown. So the user can read the option values.
$(document).ready(function () {
$('div.viewport').text($('#example')[0].textContent);
$('#example').change(function () {
$('div.viewport').text($("option:selected", this).text());
});
});
select, .container {
width: 100px;
z-index: 2;
position: relative;
}
select {
color: transparent;
background: none;
}
.container {
position: relative;
}
.viewport {
position: absolute;
width: 80%;
height: 100%;
z-index: 1;
overflow: hidden;
white-space: nowrap;
-ms-text-overflow: ellipsis;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
font-size: 14px;
padding: 3px;
}
option {
color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="viewport"></div>
<select name="example" id="example">
<option value="">This is some longggggggggggggggggggggggggggggggg text</option>
<option value="">Short Text</option>
<option value="">This is some really,really long text text and text</option>
</select>
</div>

This is code for setting title display Option text
$(function(){
$("select option").attr( "title", "" );
$("select option").each(function(i){
this.title = this.text;
})
});
$("select").tooltip({
left:25
});

In here, you may not be able to find cross browser compatible solution. Since select option element cannot be style as usual. So if you can do this grammatically it will be safe.

Related

html - make column appear after clicking on button

I've been playing around with HTML and I created a column that immediately appears when I open my file in a browser. I tried moving the column and row classes around but I can't figure out how to get it so that the column doesn't appear until after I select an option from the dropdown menu. I was wondering how I could fix this issue?
<html>
<head>
<title>Testing Display</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.center{
text-align: center;
border: 3px solid #73AD21;
}
{
box-sizing: border-box;
}
.column {
float: left;
width: 30%;
padding: 10px;
height: 2000px;
}
.row:after {
content: "";
display: table;
clear: both;
}
</style>
<body>
<div class ="center">
<p><div><h1>Testing Display</h1></div><br /><p>
<div class="dropdown">
<form>
<select name="list" id="list" accesskey="target" onchange="display(this)">
<option value="none">Choose an option</option>
<option value="one">Option one</option>
</select>
<input type=button value="Select" onclick="display()"/>
<div id="add"></div>
</form>
</div>
</div>
<div class="row">
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>Some text..</p>
<div id="div"></div>
</div>
</div>
<script src="order.js"></script>
</body>
</html>
Have the initial visibility of column to hidden.
Have a Javascript function to add a new class to column onChange. Something like this
function onSelect() {
//or add id to easily access unique single element.
var element = document.getElementByClassName("column")[0];
element.classList.add("showCol");
}
.column {
visibility: hidden;
...
}
.showCol {
visibility: visible;
...
}
Can also add styles and remove style onChange instead of toggling classes.
Javascript Solution, call this method in onchange event of drop down list:
function ddlChange(){
if (document.getElementById("list").value === "one"){
document.getElementsByClassName("column").style.display = "table-cell";
}
}
Using jQuery:
$("#list").change(function () {
if($('#list option:selected').val() === "one")
$(".column").css("display","table-cell");
});
Alternatively, you can also try to add or remove a class instead of changing inline CSS value.

Right-align dropdown menu in select2

Is there a way to right-align the dropdown menu with dropdownAutoWidth: true in a select2, instead of the standard left-align? I want the select to stay the same, but the dropdown to move to the left so it aligns with the select on the right side. See snippet below with the wrong alignment...
Note: I want different sizes of the select and the dropdown just as shown.
Edit // Updated with suggestion to add direction: rtl; text-align: right;. This only right-aligns the text. I want the whole dropdown to right-align with the selected option above it. E.g. the dropdown should stick out to the left of the selected option, not to the right.
Edit 2 // Updated with .select2-dropdown { left: -69px !important; } This makes it look the way I want, but is there a way to not have to set a fixed value of -69px?
$(".form-control").select2({
width: '100px',
dropdownAutoWidth : true,
});
.select2-container--default .select2-results > .select2-results__options { direction: rtl; text-align: right; }
.select2-dropdown {
left:-69px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
I want it to look like this, but not with a fixed value to achieve it.
As you want to have different sizes of the select and the dropdown, here is my solution using select2 built-in options and some CSS, but maybe not perfect one.
Making rtl is dead easy, just use dir, but we need to pass some other options as shown below:
$('select').select2({
dir: "rtl",
dropdownAutoWidth: true,
dropdownParent: $('#parent')
});
The key here is dropdownParent, also you need to edit your HTML as follow:
<div id='parent'>
<select>
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
</div>
And finally you need some CSS:
#parent {
/* can be any value */
width: 300px;
text-align: right;
direction: rtl;
position: relative;
}
#parent .select2-container--open + .select2-container--open {
left: auto;
right: 0;
width: 100%;
}
You can see it in action here on codepen
If you have more that one select, you need some JS code to handle dropdownParent option. btw your life will be easier if you remove dropdownAutoWidth option ;)
I managed to make it work slightly different. I needed to expand the dropdown to the left while keeping the options texts aligned left. I thought it's worth sharing. Here is the setup:
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/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>
<div id="select2-container">
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option>purple</option>
</select>
</div>
CSS
.select2-container.select2-container--default.select2-container--open {
right: auto;
left: unset !important;
}
.select2-container.select2-container--default.select2-container--open .select2-dropdown.select2-dropdown--below {
width: fit-content !important;
left: unset;
right: 0;
}
JavaScript
$('.form-control').select2({ dropdownParent: $('#select2-container') });
Result looks like:
If still looking for an alternative method. I recently needed to achieve this as well and the RTL answer was not a viable solution for my project. If you are using the select2.js(4.0.6) and do not mind modifying the script you can replace the following code and achieve right positioning with adding a class to the target element.
Look for lines 4381 to 4384
var css = {
left: offset.left,
top: container.bottom
};
Replace with the following
var containerWidth = this.$container.outerWidth()
var right = ($("body").outerWidth() - (offset.left + containerWidth));
if (this.$element.hasClass("right-align")) {
var css = {
right: right,
top: container.bottom
};
} else {
var css = {
left: offset.left,
top: container.bottom
};
}
Add the class right-align to your target select input.
<select id="client_select" class="form-control right-align">
<option value="01">Example One</option>
<option value="02">Example Two</option>
<option value="03">Example Three</option>
</select>
That should be all you need. If the target element has the class then the code will position the dropdown using right instead of left positioning. Should work the same if container is set to a parent. However, this is not thoroughly tested. It may not be an elegant solution, but it does not require overriding anything with CSS. Should do the trick until an update is made to add an option for handling right aligned dropdowns. Hope it helps.
init select with dir: "rtl" such as:
$('select').select2({
dir: "rtl,
...
...
...
...
})
option tag of select cannot be modified by css it need to be done jquery plugin
http://selectric.js.org/demo.html
It should help you my friend
I think it will help you to get what exactly you need.if u wont get it ping me back
select {
text-align-last: right;
}
option {
direction: rtl;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>

Change the line height of the selected text with Javascript

I am trying to program my own WYSIWYG editor as a summer project. I am trying to implement the line height function that controls(single spacing, double spacing, etc). I have created the dropdown that will allow users to select between the types of spacing. However, I cannot seem to get the right Javascript, because the selected text does not change in line spacing at all no matter which option I choose. Can someone please take a look at my Javascript and tell me where I went wrong? If possible, can you give me the correct code for the Javascript so I can refer off of it? Thank you!
HTML(working):
<select id="lineHeight" onchange="spacing(this)">
<option value="20px">20px</option>
<option value="80px">80px</option>
<option value="100px">100px</option>
<option value="200px">200px</option>
</select>
Javascript(not working)
function spacing(sel) {
var text = editor.document.getSelection();
text.style.lineHeight = sel.value;
}
If I understand what you're trying to do, perhaps this will work for you:
function changeStyle( property, value ) {
if ( window.getSelection().rangeCount ) {
var range = window.getSelection().getRangeAt( 0 ),
contents = range.extractContents(),
span = document.createElement( 'span' );
span.style[ property ] = value;
span.appendChild( contents );
range.insertNode( span );
window.getSelection().removeAllRanges()
}
}
#editor {
width: 350px;
max-height: 100px;
padding: 20px;
overflow-y: auto;
background-color: #efefef;
border: 1px solid #ddd
}
<p>
<label for="lineHeight">Line Height: </label>
<select id="lineHeight" onchange="changeStyle('line-height', this.value)">
<option value="20px">20px</option>
<option value="80px">80px</option>
<option value="100px">100px</option>
<option value="200px">200px</option>
</select>
<button onclick="changeStyle('font-weight', 'bold')">Bold</button>
<button onclick="changeStyle('font-style', 'italic')">Italic</button>
<button onclick="changeStyle('color', 'red')">Color</button>
<button onclick="changeStyle('background-color', 'yellow')">Background</button>
</p>
<div id="editor" contenteditable>Change the line height of the selected text with Javascript:<br>Please note that this example should be completed.</div>

Truncate text using ellipsis also truncates the tooltip

I am truncating text using ellipsis and showing the entire text on the tooltip. If the text overflows then only the tool tip is shown.
The tooltip looks fine in Chrome, but not in IE and Firefox. In IE, the tooltip text is also truncated and in firefox, the tooltip itself is cut.
<div class="card">
<p>From:</p>
<p> Dark Angel </p>
<p class="ellipsis"> QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQAAAAAA
New york, US<p>
<div>
CSS:
.card {
height:416px;
width:280px;
display:block;
border: 1px solid black;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
}
jQuery:
$('p.ellipsis').bind('mouseenter', function () {
var $this = $(this);
if (this.offsetWidth < this.scrollWidth && !$this.attr('title'))
$this.attr('title', $this.text());
You can give the element a different class name and then use Javascript to update it, setting the relevant ellipsis class name, as well as capturing the text at that time and storing it as a data attribute of the element, so you can access it later.
Note that I changed the <p> tag to have the class name pre-ellipsis...
// add a data-title attribute with the original text, then modify the class list
// to remove pre-ellipsis and add ellipsis
$("p.pre-ellipsis").each(function() {
var $this = $(this);
$this.data("title", $this.text());
$this.removeClass("pre-ellipsis").addClass("ellipsis");
});
// your original code, but modified to get the tooltip text from the data attribute
$("p.ellipsis").on("mouseenter", function () {
var $this = $(this);
if (this.offsetWidth < this.scrollWidth && !$this.attr('title')) {
$this.attr('title', $this.data("title"));
}
});
.card {
height:416px;
width:280px;
display:block;
border: 1px solid black;
}
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="card">
<p>From:</p>
<p> Dark Angel </p>
<p class="pre-ellipsis"> QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQAAAAAA
New york, US<p>
<div>

Style Select Element with same style as the selected option

I am trying to style a select element that is created in php. The select code is very simple and I can style the text color of the drop down. But this doesn't style the select element when a option is selected. Also this code doesn't work on Safari at all.
Code:
<div class="theme">
<select class="theme" name="select_theme">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
CSS:
.theme {
width: 200px;
}
.theme select {
width: 200px;
background-color: rgba(200,200,200,0.8);
}
.theme select option[value="red"] {
color: red;
}
.theme select option[value="green"] {
color: green;
}
.theme select option[value="blue"] {
color: blue;
}
How do I style the default select so that it matches the option style rule?
How to get this to work on Safari? Possibly IE I haven't checked it yet no Windows system ATM.
Is there a way to get these rules to display an image as well? I've tried a few things but seems it requires Javascript which I try to avoid when I can.
I made a JSFiddle to demonstrate this.
There's no way you can accomplish this without JavaScript:
$('select.theme').change(function () {
$(this).css('color', $(this).find('option:selected').css('color'));
}).change();
Here's your fiddle: http://jsfiddle.net/uCmSR/2/

Categories

Resources