Last active
August 29, 2015 14:03
-
-
Save fnalin/a11ebe0ca5bb92780f15 to your computer and use it in GitHub Desktop.
Div editável dinamica
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Demo - div-container-plus</title> | |
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" /> | |
<link href="div-container-plus.css" rel="stylesheet" /> | |
</head> | |
<body> | |
<p class="text-center">Clique sob a Div abaixo e pressione enter para adicionar item à lista</p> | |
<div id="div-container-plus" contenteditable> | |
<ul contenteditable="false" class="list-inline"> | |
<li contenteditable class="novo-li" data-maxlength="50"></li> | |
</ul> | |
</div> | |
</body> | |
</html> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="div-container-plus.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
div#div-container-plus { | |
border: 1px solid #ccc; | |
margin: 10px; | |
border-radius: 4px; | |
outline: 0; | |
-moz-appearance: textfield-multiline; | |
-webkit-appearance: textarea; | |
font: medium -moz-fixed; | |
font: -webkit-small-control; | |
} | |
.close { | |
float: none !important; | |
font-size: 20px !important; | |
vertical-align: middle; | |
} | |
div#div-container-plus > ul { | |
padding: 5px 0 0 15px; | |
} | |
div#div-container-plus > ul > li { | |
margin: 2px 1px; | |
padding: 5px; | |
background-color: #CCE3F4; | |
border-radius: 4px; | |
} | |
div#div-container-plus > ul > li.novo-li { | |
background-color: #fff; | |
} | |
.invalido { | |
margin: 0 !important; | |
border: 2px solid #ff0000; | |
outline: 0; | |
background-color: rgba(255, 0, 110, 0.1) !important; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
by Fabiano Nalin - FabianoNalin.net.br | |
07/2014 | |
*/ | |
var maxlength = 0; | |
$("#div-container-plus").on('click', function () { | |
$(".novo-li").focus(); | |
}); | |
$(".novo-li").on('keypress', function (e) { | |
var total = $(this).text().length; | |
maxlength = $(this).data("maxlength"); | |
if (total >= maxlength) { | |
$(this).addClass("invalido"); | |
} | |
if (e.which == 13) { | |
e.preventDefault(); | |
if (total <= maxlength) { | |
var descricao = $(this).text().trim(); | |
var descricao_texto = "<input type='hidden' name='Descricao' value='0|" + descricao + "' />"; | |
var li = "<li>" + descricao_texto + descricao + " <button type='button' class='close'>×</button></li></li>"; | |
$(this).text(''); | |
$(li).insertBefore(".novo-li"); | |
} | |
} | |
}); | |
$(".novo-li").on('keyup', function (e) { | |
var total = $(this).text().length; | |
if (total <= maxlength && $(this).hasClass("invalido")) { | |
$(this).removeClass("invalido"); | |
} | |
}); | |
$("ul").on('click', '.close', function (e) { | |
e.preventDefault(); | |
$(this).parent().remove(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment