javascript liste sur plusieurs lignes [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a list of items contained in a dynamically created div. Unfortunately when the list is too long, it overflows the div. Is there a way to get a newline when the list overflows?
CSS:
div.StdDiv{
width: 30%;
background-color: #ffcc00;
text-align: center;
font-size: 100%;
color: #000000;
padding: 0.5em;
border: 1px solid #ccc;
margin-bottom: 1px;
}
ul.boxy {
list-style:none;
padding: 0px;
margin: 0px;
height: auto;
text-align: center;
}
ul.boxy li {
cursor:move;
display:inline;
margin-right: 2px;
margin-bottom: 2px;
padding:2px 6px 2px 6px;
border: 1px solid silver;
background-color: #eee;
font-size: 100%;
}
HTML:
<div id="divListMots" class="StdDiv">
<span id="ListMots">la la lere</span>
</div>
JavaScript:
var numlist = ['0','1','2','3','4','5','6','7','8','9'];
var textlist = ['li0','li1','li2','li3','li4','li5','li6','li7','li8','li9'];
var ListMots ='';
var ListOfWordsStart = '<ul class="boxy" id="ul">   ';
var ListOfWordsEnd = '</ul>';
var LiWordStart = '<li id="box' ;
var LiWordEnd = ' " class="">' ;
var LiWord ='';
var sep = '    ';
var Output = '';
for (var k=0; k<numlist.length; k++) // pour tous les trous
{
var num = numlist[k];
var WordText = textlist[num];
LiWord = LiWordStart + num + LiWordEnd + WordText +'</li>' + sep;
Output = Output + LiWord ;
}
ListOfWords = ListOfWordsStart + Output + ListOfWordsEnd;
document.getElementById('ListMots').innerHTML = ListOfWords;
jsfiddle

Question in english:
Hello,
I have a list of items contained in a dynamically created div.
Unfortunately when the list is too long, it overflows the div. Is there
a way to get a newline when the list overflows?
The easiest way to accomplish this would be to use
display: inline-block;
instead of
display: inline;
New CSS:
ul.boxy li {
cursor:move;
display:inline-block;
margin-right: 2px;
margin-bottom: 2px;
padding:2px 6px 2px 6px;
border: 1px solid silver;
background-color: #eee;
font-size: 100%;
}
Updated jsFiddle
You should note, however, that this may not work with older browsers.

Related

How to make a text input field take in keywords? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In semantic UI, when using the dropdown component, once you make a selection, it selects the word and appears as a keyword select as shown, allowing for multiple selections:
How can this be done with a text field that takes in any input, and after the user presses enter it will create a selection in the gray box similar to semantic ui? I need to allow this for multiple custom text inputs. Thanks in advance to anyone that can help.
Your description inspired me to implement this feature. Here's what I came up with:
function multiSearchKeyup(inputElement) {
if(event.keyCode === 13) {
inputElement.parentNode
.insertBefore(createFilterItem(inputElement.value), inputElement)
;
inputElement.value = "";
}
function createFilterItem(text) {
const item = document.createElement("div");
item.setAttribute("class", "multi-search-item");
const span = `<span>${text}</span>`;
const close = `<div class="fa fa-close" onclick="this.parentNode.remove()"></div>`;
item.innerHTML = span+close;
return item;
}
}
.multi-search-filter{
border:1px solid #DDD;
border-radius: 3px;
padding:3px;
min-height: 26px;
}
.multi-search-filter > input {
border: 0px;
outline: none;
font-size: 20px;
}
.multi-search-item {
margin: 2px;
padding: 2px 24px 2px 8px;
float: left;
display: flex;
background-color: rgb(204, 204, 204);
color: rgb(51, 51, 51);
border-radius: 3px;
position: relative;
}
.multi-search-item > span {
font-family: 'Muli';
line-height: 18px;
}
.multi-search-item > .fa {
font-size: 12px;
line-height: 18px;
margin-left: 8px;
position: absolute;
right: 8px;
top: 2px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://fonts.googleapis.com/css?family=Muli&display=swap" rel="stylesheet">
<div class="multi-search-filter" onclick="Array.from(this.children).find(n=>n.tagName==='INPUT').focus()">
<input type="text" onkeyup="multiSearchKeyup(this)">
</div>

How do I append data from localStorage to an element on the DOM

Currently working on a Bookmark manager that saves data to localstorage, then append the stored data from the localstorage to the DOM but the fetchUrl() doesn't append the code to the DOM.
Please find below what I have tried:
document.getElementById('subButton').addEventListener('click',storeFormInput)
var bookmarks= []
function storeFormInput(e){
e.preventDefault()
//get input values
var nameInput = document.getElementById('name').value;
var urlInput = document.getElementById('url').value;
//create a bookmark object
let bookmark = {
name: nameInput,
url: urlInput
}
// store bookmark in bookmarks array
bookmarks.push(bookmark);
localStorage.setItem('bookmarksKey',JSON.stringify(bookmarks));
fetchUrl()
}
function fetchUrl(){
var displayResult = document.getElementById('result');
var localDb = JSON.parse(localStorage.getItem('bookmarksKey'))
displayResult += localDb
}
body{
background: #EFEFEF
}
.content{
margin: auto;
margin-top: 70px;
width: 600px;
text-align: center;
box-sizing: border-box;
filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.161));
fill: rgba(255,255,255,1);
background: #fff;
padding-top: 30px;
padding-bottom: 30px;
}
input{
border-radius: 3px;
}
input[type="text"]{
color: red;
margin-top: 10px;
padding: 10px;
display: block;
border: none;
filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.161));
fill: rgba(255,255,255,1);
background: #fff;
width: 50%;
margin: auto;
}
#subButton{
width: 30%;
padding: 10px;
margin: auto;
margin-top: 30px;
text-align: center;
border: none;
}
.blue{
background-color: #3D78FF;
color: #fff;
}
input[type="button"]:hover{
}
#saved{
background:#20c65b !important;
box-sizing: border-box;
filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.161));
fill: rgba(255,255,255,1);
background: #fff;
border-radius: 3px;
width: 250px;
text-align: center;
padding:1px 5px;
color: #fff;
}
#resultPanel{
display: block;
background: #fff;
box-sizing: border-box;
filter: drop-shadow(0px 3px 6px rgba(0, 0, 0, 0.161));
fill: rgba(255,255,255,1);
background: #fff;
padding: 10px;
margin-top: 20px;
width: 250px;
}
#visit{
}
<div class="content">
<h1>Website Bookmarker</h1>
<form id="form1">
<label for="">Name: </label>
<input type="text" name="" id="name" placeholder="Website">
<br>
<label for="">Url: </label><input type="text" name="" id="url" placeholder="Website">
<button class="blue" id="subButton" type="button">Submit</button>
</form>
</div>
<div id="displayResult">
<div id="saved">
<h3>SAVED BOOKMARK</h3>
</div>
<div id="result">
</div>
</div>
P.S I tried calling fetchUrl() on body load like
<body onload="fetchUrl()">
However, whenever I click the submit button, it doesn't append the localDb to the DOM until I refresh the page then it duplicates all the data stored in local storage in the DOM.
With this:
var displayResult = document.getElementById('result');
you get a reference to the dom element. So you can work with the dom element API . Display it in a simple way:
displayResult.innerHTML = '<pre>' + localStorage.getItem('bookmarksKey') + '</pre>'
But if you want to display it in a beatiful way, you have to go through your data and build element with the api.
So first study the api.
You can use below code snippet
window.onload = function() {
fetchUrl();
};
document.getElementById('subButton').addEventListener('click',storeFormInput)
var bookmarks= []
function storeFormInput(e){
e.preventDefault()
//get input values
var nameInput = document.getElementById('name').value;
var urlInput = document.getElementById('url').value;
//create a bookmark object
let bookmark = {
name: nameInput,
url: urlInput
}
// store bookmark in bookmarks array
bookmarks.push(bookmark);
localStorage.setItem('bookmarksKey',JSON.stringify(bookmarks));
fetchUrl()
}
function prepareHTML(data){
var str = ''
if(data.length){
str += '<table>'
for(var i=0; i<data.length; i++){
str += '<tr>'
str += '<td>' + data[i].name + '</td>'
str += '<td>' + data[i].url + '</td>'
str += '</tr>'
}
str += '</table>'
}
return str
}
Prepare HTML from Array of elements and set in innerHTML of results div...

How can I create a Help popup [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to add a small box next to a question on a form I am asking which when clicked will pop up with a box of text for help about the question. How can i add some java script to create a Pop-up box that will appear when clicked rather than using on mouse over
Design it as you like.
Note that you can add several help popups. Just add class="help" and a title containing the help text.
var helpBox = document.getElementById("helpBox");
var helpElements = document.getElementsByClassName("help");
for (var i = 0; i < helpElements.length; i++) {
helpElements[i]._helpText = helpElements[i].title;
helpElements[i].removeAttribute("title");
helpElements[i].onclick = function(e) {
helpBox.style.display = "block";
helpBox.innerHTML = "<span id='close' title='Close'>X</span>" +
e.target._helpText;
helpBox.children[0].onclick = function() {
helpBox.style.display = "none";
}
}
}
body {
background-color: #f4f4f4;
font-family: sans-serif;
}
.help {
cursor: help;
}
#helpBox {
position: absolute;
top: 100px;
display: none;
width: 300px;
left: 50%;
margin-left: -150px;
border: 1px solid gray;
padding: 10px;
background-color: white;
z-index: 1000;
}
#helpBox #close {
float: right;
cursor: pointer;
background-color: red;
color: white;
padding: 0 6px;
}
<span class="help" title="Help text">Help</span>
<div id="helpBox"></div>

How to solve this? [] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
why this problem occurs.I am added a tag element(number) in Hoursdiv through
dynamically.but the problem is:- element is not added in proper way.after added 6
element, space created.
javascript code:-
for(var i=1 ; i<=12; i++) {
(function () {
var cc=i;
if(cc<=9)
cc='0'+i;
var _id1 = "time"+cc;
str1 += "<a id='"+_id1+"' class='hournum a_Cal' >"+cc+"</a>";
}());
}
css code (h2ooooooo edit note: I have no idea if 'a_cal' is actually in the css file, but I've kept it here in case):
'a_cal'
.a_Cal{
text-decoration: none;
color: black;
width: 18%;
height: 11%;
float: left;
border-radius: 4px;
}
.hournum{
padding:5px;
}
.hoursdiv{
border: 1px solid #6699FF;
position: absolute;
top: 25%;
left: 4%;
text-align: center;
color: red;
width: 45%;
height: 52%;
background-color: white;
}
Hm, I feel dirty looking at your code.
If your problem is that space on the right side of the container then let me explain what's going on.
The reason that space is there is because the 6th element is too wide to stay on the same "row", and it gets pushed down to the next row. The reason it's too wide is because of the combined use of %-width and padding in your css, which never ends well.
Illustration
I've constructed an example of how I think you want it to look.
I also cleaned your code a little ;)
Example | Code
In this example I use absolute values instead of percentages. The benefit from this is that I can calculate the exact width required for my container.
A link's width is the sum of the width + padding + margin + border:
40 + (5*2) + (2*2) + (1*2) = 56 px
And we want 5 links per "row"
56 * 5 = 280 px
So the container's width has to be at least 280px to fit 5 links per row.
CSS
.hournum{
text-decoration: none;
color: black;
width: 40px;
float: left;
padding: 0px 5px;
text-align: center;
color: red;
border-radius: 3px;
border: 1px solid black;
margin: 2px 2px;
}
.container{
position: absolute;
top: 10px;
left: 10px;
width: 280px;
background-color: white;
border: 1px solid #6699FF;
padding: 2px;
}
Javascript
var str = "";
for(var i=1 ; i<=12; i++){
var cc = i;
var row
if(i <= 9) cc = "0" + i
str += ''+cc+"";
}
document.body.innerHTML = '<div class="container">'+str+"</div>";
Result

Match string and remove associated CSS [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
So I have a Javascript array of CSS selectors:
array is like the following: '.header', '#footer', '#nav', etc
I also have a div containing my site CSS as text (12,000 lines long):
<div id="all">
#nav
{
border: 1px solid;
color: black;
}
#footer
{
display: block;
}
</div>
I need to take each selector from the array and for its match in $('#all'), remove everything from the start of the selector to it's end bracket.
i.e.
// for each from the array
$('#all').find($(this));
// removes everything from selector to its end bracket
Can anyone advise? It's to remove unused site CSS. I've been stuck for a while. Appreciated!
Update: Added debug mode, input, and output.
Okay, I guess you have a bunch of CSS and you wanna remove a few rules. Let me assume these:
Each row has only one selector.
Each row starts with that selector.
Each row has one complete rule. i.e., selector {property: value;}
If the third option is not succeeding, there are a lot of CSS compressors, and you can use them to make this CSS:
#footer
{
display: block;
}
Into:
#footer { display: block; }
Now once you have this in a text file, you can do the manipulation using PHP or some good programming language. I do not recommend client side one. Say if you have PHP, and you have the set of rules here:
#nav { border: 1px solid; color: black; }
#footer { display: block; }
And you have your array:
$remove = array('.header', '#footer', '#nav');
You can use something like this:
<pre><?php
$debug = false;
$rules[] = ".header { border: 1px solid; color: black; }";
$rules[] = "#nav { border: 1px solid; color: black; }";
$rules[] = "#footer { display: block; }";
$rules[] = "#header { border: 1px solid; color: black; }";
$rules[] = ".nav { border: 1px solid; color: black; }";
$rules[] = ".footer { display: block; }";
$remove = array('.header', '#footer', '#nav');
$final = array();
foreach ($rules as $rule)
{
if ($debug) echo "Debug: ", var_dump(strpos($rule, " ")) . "\n";
if ($debug) echo "Debug: ", var_dump(substr($rule, 0, strpos($rule, " "))) . "\n";
if ($debug) echo "Debug: ", var_dump(in_array(substr($rule, 0, strpos($rule, " ")), $remove)) . "\n";
if ($debug) echo "---\n";
if ( !in_array(substr($rule, 0, strpos($rule, " ")), $remove) )
$final[] = $rule;
}
echo implode("\n", $final);
?></pre>
The above script includes a debug. If you set $debug to true, you can see the different stuffs being checked.
The input given to this is:
.header { border: 1px solid; color: black; }
#nav { border: 1px solid; color: black; }
#footer { display: block; }
#header { border: 1px solid; color: black; }
.nav { border: 1px solid; color: black; }
.footer { display: block; }
The output is:
#header { border: 1px solid; color: black; }
.nav { border: 1px solid; color: black; }
.footer { display: block; }
Fiddle: http://phpfiddle.org/main/code/ehd-z4j

Categories

Resources