I have the beginning of I note taking website I am making as an experiment. It was working fine in the live preview from brackets, but then I saved the files and now none of the jQuery works. I think it is my code by it could also be something with Brackets since it only happened once I saved. Here is my code:
$(document).ready(function() {
var notes = [];
$("#newNoteWrapper").hide();
$(".text").focus(function() {
$(this).stop
$(this).animate({
marginLeft: "+=3%"
});
});
$(".text").blur(function() {
$(this).stop
$(this).animate({
marginLeft: "-=3%"
});
});
$(".button").hover(function() {
$(this).stop();
$(this).animate({
backgroundColor: "#108cb1"
}, 200);
}, function() {
$(this).stop()
$(this).animate({
backgroundColor: "#2DBEEB"
}, 200);
});
$("#newNoteButton").click(function() {
newNote(true);
});
$("#doneButton").click(function() {
newNote(false);
});
});
var newNote = function(enter) {
if enter {
$("#newNoteWrapper").show();
} else {
$("#newNoteWrapper").hide();
notes.push({
title: $("#noteTitle").val(),
body: $("#noteBody").val(),
});
}
}
html,
body {
padding: 0;
margin: 0;
border: 0;
background-color: #FFFFFF;
font-family: sans-serif;
}
.button {
padding: 15px 25px;
border: none;
font-size: 25px;
margin-bottom: 10px;
background-color: #2DBEEB;
color: #FFFFFF;
outline: none;
}
.text {
border: none;
outline: none;
width: 92%;
border-left: 10px solid #2DBEEB;
resize: vertical;
background-color: #FFFFFF;
}
#newNoteButton {
margin: 10px;
}
#newNoteWrapper {
z-index: 2;
position: absolute;
height: 100%;
width: 100%;
background-color: #FFFFFF;
top: 0%;
}
#newNote {
background-color: #2DBEEB;
color: #FFFFFF;
font-size: 30px;
padding: 10px;
text-align: center;
}
#noteTitle {
font-size: 25px;
height: 50px;
margin: 10px 0px 10px 0px;
}
#noteBody {
font-size: 12pt;
height: 500px;
margin: 0px 0px 10px 0px;
}
#doneButton {
left: 0;
position: absolute;
margin-left: 10px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="notes.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.color-animation/1/mainfile"></script>
<script src="notes.js"></script>
<title>Simple Notes</title>
</head>
<body>
<button id="newNoteButton" class="button">new note</button>
<div id="newNoteWrapper">
<div id="newNote">new note</div>
<form>
<input type="text" id="noteTitle" class="text" placeholder="title..."></input>
<textarea id="noteBody" class="text" placeholder="body..."></textarea>
<br />
<button id="doneButton" class="button">done</button>
</form>
</div>
</body>
</html>
Is there something wrong with this or should I try a different editor?
you must edit the follwing of
In HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.color-animation/1/mainfile"></script>
In JS
var newNote = function(enter) {
if (enter) {
$("#newNoteWrapper").show();
} else {
$("#newNoteWrapper").hide();
notes.push({
title: $("#noteTitle").val(),
body: $("#noteBody").val(),
});
}
}
Related
I have this star rate form and wanted you to start typing in the text area and it will show you the star rate form right away, but if the text area is empty, it will hide.
I discovered the following code:
<HTML>
<input placeholder="Enter some text" name="name" />
<p id="values"></p>
<JS>
const input = document.querySelector('input');
const log = document.getElementById('values');
input.addEventListener('input', updateValue);
function updateValue(e) {
log.textContent = e.target.value;
}
How can I combine this code into mine? Because it only does that now when I click outside of the text area and then show the stars. Please advise me on this!
#import url(https://fonts.googleapis.com/css?family=Roboto:500,100,300,700,400);
* {
margin: 0;
padding: 0;
font-family: roboto;
}
body {
background: #000;
}
.cont {
width: 93%;
max-width: 350px;
text-align: center;
margin: 4% auto;
padding: 30px 0;
background: #111;
color: #EEE;
border-radius: 5px;
border: thin solid #444;
}
hr {
margin: 20px;
border: none;
border-bottom: thin solid rgba(255, 255, 255, .1);
}
div.title {
font-size: 2em;
}
h1 span {
font-weight: 300;
color: #Fd4;
}
div.stars {
width: 270px;
display: inline-block;
}
input.star {
display: none;
}
label.star {
float: right;
padding: 10px;
font-size: 36px;
color: #444;
transition: all .2s;
}
input.star:checked~label.star:before {
content: '\f005';
color: #FD4;
transition: all .25s;
}
input.star-5:checked~label.star:before {
color: #FE7;
text-shadow: 0 0 20px #952;
}
input.star-1:checked~label.star:before {
color: #F62;
}
label.star:hover {
transform: rotate(-15deg) scale(1.3);
}
label.star:before {
content: '\f006';
font-family: FontAwesome;
}
.rev-box {
height: 0;
width: 100%;
transition: all .25s;
}
textarea.review {
background: #222;
border: none;
width: 50%;
max-width: 100%;
height: 50px;
padding: 10px;
box-sizing: border-box;
color: #EEE;
}
label.review {
display: block;
transition: opacity .25s;
}
input.star:checked~.rev-box {
height: 125px;
overflow: visible;
}
<html>
<head>
<link rel="stylesheet" href="startratecss.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.2.0/jquery.rateyo.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.2.0/jquery.rateyo.min.js"></script>
</head>
<body>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" />
<div class="cont">
<div class="stars">
<form>
<div class="rev-box">
<textarea class="review" id="review" cols="2" rows="2" name="review" onchange="asd(this)"></textarea>
</div>
<div id="rateYo" style="visibility: hidden;"></div>
<input type="hidden" name="rating" id="rating_input" />
</form>
</div>
</div>
<script>
function asd(txt) {
var something = txt.value;
var star = document.getElementById("rateYo");
star.style.visibility = "visible";
}
$(function MyFunction() {
$("#rateYo").rateYo({
onSet: function(rating, rateYoInstance) {
rating = Math.ceil(rating);
$('#rating_input').val(rating); //setting up rating value to hidden field
var bod = document.getElementById("review").value;
window.location.href = 'mailto:your#gmail.com?subject=' + rating + ' of 5' + '&body=' + bod;
}
});
});
</script>
</body>
</html>
I have taken the liberty of rewriting your code to accomplish what you are trying to do.
Two tips I want to give you:
Decouple the data layer with the presentation layer. You are mixing CSS styles in stylesheets, HTML and JavaScript. You are also having JavaScript as strings in your HTML attributes. This gets confusing quite rapidly! We have gone a long way since HTML4 and the 90's!
If you use a library, then use it. For example, you are using jQuery, then use it as it should instead of adding event handlers directly to the HTML attributes; jQuery has $(element).on(event, handler), don't do <div onclick="something(this)"></div>.
$(function MyFunction() {
const textInput = $("#review").on('input', function () {
if (textInput.val().length) {
rateStar.show();
} else {
rateStar.hide();
}
});
const ratingInput = $('#rating_input');
const rateStar = $("#rateYo").rateYo({
onSet: function(rating, rateYoInstance) {
let inputValue = textInput.val();
ratingInput.val(Math.ceil(rating)); //setting up rating value to hidden field
console.log( rating, inputValue );
// window.location.href = 'mailto:your#gmail.com?subject=' + rating + ' of 5' + '&body=' + bod;
}
}).hide();
});
#import url(https://fonts.googleapis.com/css?family=Roboto:500,100,300,700,400);
* {
margin: 0;
padding: 0;
font-family: roboto;
}
body {
background: #000;
}
.cont {
width: 93%;
max-width: 350px;
text-align: center;
margin: 4% auto;
padding: 30px 0;
background: #111;
color: #EEE;
border-radius: 5px;
border: thin solid #444;
}
hr {
margin: 20px;
border: none;
border-bottom: thin solid rgba(255, 255, 255, .1);
}
div.title {
font-size: 2em;
}
h1 span {
font-weight: 300;
color: #Fd4;
}
div.stars {
width: 270px;
display: inline-block;
}
label.star {
float: right;
padding: 10px;
font-size: 36px;
color: #444;
transition: all .2s;
}
input.star:checked~label.star:before {
content: '\f005';
color: #FD4;
transition: all .25s;
}
input.star-5:checked~label.star:before {
color: #FE7;
text-shadow: 0 0 20px #952;
}
input.star-1:checked~label.star:before {
color: #F62;
}
label.star:hover {
transform: rotate(-15deg) scale(1.3);
}
label.star:before {
content: '\f006';
font-family: FontAwesome;
}
.rev-box {
/* height: 0; */
width: 100%;
transition: all .25s;
}
textarea.review {
background: #222;
border: none;
width: 50%;
max-width: 100%;
height: 50px;
padding: 10px;
box-sizing: border-box;
color: #EEE;
}
label.review {
display: block;
transition: opacity .25s;
}
input.star:checked~.rev-box {
height: 125px;
overflow: visible;
}
<html>
<head>
<link rel="stylesheet" href="startratecss.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.2.0/jquery.rateyo.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.2.0/jquery.rateyo.min.js"></script>
</head>
<body>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" />
<div class="cont">
<div class="stars">
<form>
<div class="rev-box">
<textarea class="review" id="review" cols="2" rows="2" name="review"></textarea>
</div>
<div id="rateYo"></div>
<input type="hidden" name="rating" id="rating_input" />
</form>
</div>
</div>
</body>
</html>
I am trying to learn coding in my freetime and have run into an issue. I am trying to have 'contact info' to be a on hover on profile picture so that it shows a way to contact me via phone, email, and shows my name. I'm hoping someone can help with this please.
Cut most of the HTML code in the process to allow post
var infoDivs = document.getElementsByClassName ('infoDivs');
var contactInfoDiv = document.getElementById('contactInfo');
function displayInfo(index) {
for (var i = 0; i < infoDivs.length; i++) {
infoDivs[i].style.display = 'none';
}
infoDivs[index].style.display = 'block';
}
function showcontactInfo() {
contactInfoDiv.style.display = 'block'
}
function hidecontactInfo() {
contactInfoDiv.style.display = 'none'
}
//line 12-18 is what should be my issue? Otherwise line 2? //
html, body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
color: white;
font-family: "verdana";
}
body {
background-image: url('https://imgur.com/GfUxQA7.jpg');
padding: 10px;
}
h1 {
font-size: 35px;
}
h2 {
margin: 0px;
font-size: 30px;
}
header {
margin: 20px;
height: 20%;
}
header img {
position: absolute;
right: 20px;
border-style: solid;
border-radius: 50%;
border-width: 2px;
}
header p {
margin-top: 50px;
}
#main {
position: relative;
height: 70%;
}
#navlist {
list-style: none;
display: inline-block;
height: 100%;
padding: 0px;
margin: 20px;
width: 25%;
font-size: 20px;
font-weight: bold;
}
#navlist li {
height: 18%;
}
#navlist li:hover {
background-color: rgba(255, 255, 255, 0.2);
}
#infoContainer {
position: absolute;
display: inline-block;
top: 20px;
bottom: 20px;
right: 10%;
left: 25%;
height: 90%;
margin-left: 20px;
background-color: rgba(255, 255, 255, 0.2);
}
.infoDivs {
display: none;
margin: 20px;
}
.infoDivs h2 {
margin-bottom: 40px;
}
#bioDiv {
display: block;
}
#contactInfo {
background-color: white;
display: none;
position: fixed;
width: 50%;
left: 25%;
top: 30%
color: black;
text-align: center;
font-size: 3vw;
}
#media (max-width: 500px) {
#navList {
font-size: 15px;
margin-left: 10px;
}
#infoContainer {
margin-left: 10px;
}
}
#media (max-width: 400px) {
h1 {
font-size: 30px;
}
header img {
width: 50px;
right: 5px;
}
#navList {
font-size: 12px;
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS for my webpage.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>My webpage</title>
</head>
<body>
<script src="Javascript html.js"></script>
<header>
<img onmouseover='showcontactInfo()' onmouseout='hidecontactInfo()' src='Profile.jpg' alt="Profile picture"> <!--This is what line im having issue with i think?-->
<h1>My Webpage</h1>
<p>Welcome to my webpage where you can learn about me!</p>
</header>
<div id='main'>
<ul id='navlist'>
<li onclick='displayInfo(0)'>Bio</li>
<li onclick='displayInfo(1)'>Education</li>
<li onclick='displayInfo(2)'>Work</li>
<li onclick='displayInfo(3)'>Projects</li>
<li onclick='displayInfo(4)'>Interests</li>
</ul>
<div id='infoContainer'>
<div id='bioDiv' class='infoDivs'>
<h2>Bio</h2>
<p>Here is a little about myself and my background.</p>
<p>Hello my name is Antoly, I'm currently learning coding (HTML5, CSS, and Javascript) and teaching myself Italian language in my free time and attending school to become a Information Technology Network Specialist</p>
</div>
<div id='educationDiv' class='infoDivs'>
<h2>Education</h2>
<p>Here is some info about my schooling and courses that I've taken.</p>
</div>
</div>
</div>
</div>
<div id='contactInfo'> <!-- This is what I want to show my info when you put your mouse over my picture-->
<p>Antoly Borshkev</p>
<p>helloworld#gmail.com</p>
<p>1111111111</p>
</div>
</body>
</html>
You've missed ; after top: 30%; in CSS #contactInfo class
var infoDivs = document.getElementsByClassName ('infoDivs');
var contactInfoDiv = document.getElementById('contactInfo');
function displayInfo(index) {
for (var i = 0; i < infoDivs.length; i++) {
infoDivs[i].style.display = 'none';
}
infoDivs[index].style.display = 'block';
}
function showcontactInfo() {
contactInfoDiv.style.display = 'block'
}
function hidecontactInfo() {
contactInfoDiv.style.display = 'none'
}
//line 12-18 is what should be my issue? Otherwise line 2? //
html, body {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
color: white;
font-family: "verdana";
}
body {
background-image: url('https://imgur.com/GfUxQA7.jpg');
padding: 10px;
}
h1 {
font-size: 35px;
}
h2 {
margin: 0px;
font-size: 30px;
}
header {
margin: 20px;
height: 20%;
}
header img {
position: absolute;
right: 20px;
border-style: solid;
border-radius: 50%;
border-width: 2px;
}
header p {
margin-top: 50px;
}
#main {
position: relative;
height: 70%;
}
#navlist {
list-style: none;
display: inline-block;
height: 100%;
padding: 0px;
margin: 20px;
width: 25%;
font-size: 20px;
font-weight: bold;
}
#navlist li {
height: 18%;
}
#navlist li:hover {
background-color: rgba(255, 255, 255, 0.2);
}
#infoContainer {
position: absolute;
display: inline-block;
top: 20px;
bottom: 20px;
right: 10%;
left: 25%;
height: 90%;
margin-left: 20px;
background-color: rgba(255, 255, 255, 0.2);
}
.infoDivs {
display: none;
margin: 20px;
}
.infoDivs h2 {
margin-bottom: 40px;
}
#bioDiv {
display: block;
}
#contactInfo {
background-color: white;
display: none;
position: fixed;
width: 50%;
left: 25%;
top: 30%; /* missed ; */
color: black;
text-align: center;
font-size: 3vw;
}
#media (max-width: 500px) {
#navList {
font-size: 15px;
margin-left: 10px;
}
#infoContainer {
margin-left: 10px;
}
}
#media (max-width: 400px) {
h1 {
font-size: 30px;
}
header img {
width: 50px;
right: 5px;
}
#navList {
font-size: 12px;
}
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS for my webpage.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>My webpage</title>
</head>
<body>
<script src="Javascript html.js"></script>
<header>
<img onmouseover='showcontactInfo()' onmouseout='hidecontactInfo()' src='https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w100-h50-n-l50-sg-rj' alt="Profile picture"> <!--This is what line im having issue with i think?-->
<h1>My Webpage</h1>
<p>Welcome to my webpage where you can learn about me!</p>
</header>
<div id='main'>
<ul id='navlist'>
<li onclick='displayInfo(0)'>Bio</li>
<li onclick='displayInfo(1)'>Education</li>
<li onclick='displayInfo(2)'>Work</li>
<li onclick='displayInfo(3)'>Projects</li>
<li onclick='displayInfo(4)'>Interests</li>
</ul>
<div id='infoContainer'>
<div id='bioDiv' class='infoDivs'>
<h2>Bio</h2>
<p>Here is a little about myself and my background.</p>
<p>Hello my name is Antoly, I'm currently learning coding (HTML5, CSS, and Javascript) and teaching myself Italian language in my free time and attending school to become a Information Technology Network Specialist</p>
</div>
<div id='educationDiv' class='infoDivs'>
<h2>Education</h2>
<p>Here is some info about my schooling and courses that I've taken.</p>
</div>
</div>
</div>
</div>
<div id='contactInfo'> <!-- This is what I want to show my info when you put your mouse over my picture-->
<p>Antoly Borshkev</p>
<p>helloworld#gmail.com</p>
<p>1111111111</p>
</div>
</body>
</html>
This is the script, it work fine as you can see but this reset functionality have some issue, it turns the value to 0 but when again increase the value it starts from the last stored value, whats the issue?
This is the CSS of the counter application this we help you to run this file.
Ans this is the html file is pretty much good but just in issue with script
var counter = 0;
$("#box").text(counter);
function reset() {
counter = 0;
$("#box").text(counter);
}
function inc() {
counter = counter + 1;
$("#box").text(counter);
}
function dec() {
counter = counter - 1;
$("#box").text(counter);
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
height: 100vh;
}
.main {
background: #efefef;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
padding: 30px;
}
.main h1 {
margin-bottom: 50px;
}
#box {
height: 50px;
line-height: 50px;
background: #efefef;
text-align: center;
margin-top: 40px;
box-shadow: 1px 1px 5px #000;
font-size: 30px;
}
.btn {
display: flex;
}
.inc,
.dec {
margin: 10px;
padding: 10px;
background: #fcfbfc;
margin-top: 30px;
text-transform: uppercase;
cursor: pointer;
}
.reset {
background: orange;
padding: 5px;
margin-top: 10px;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="counter.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<title>Counter</title>
</head>
<body>
<div class="main">
<h1 style="font-family: sans-serif;">Counter App</h1>
<div id="box">
</div>
<div class="btn">
<div class="inc" id="inc" onclick="inc();">
Increase
</div>
<div class="dec" id="dec" onclick="dec();">
Decrease
</div>
</div>
<div class="reset" onclick="reset();">
RESET
</div>
</div>
</body>
</html>
Added reset counter variable to 0 in reset() function.
var counter = 0;
$("#box").text(counter);
function reset() {
counter = 0;
$("#box").text(0);
}
function inc() {
counter = counter + 1;
$("#box").text(counter);
}
function dec() {
counter = counter - 1;
$("#box").text(counter);
}
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
height: 100vh;
}
.main {
background: #efefef;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
padding: 30px;
}
.main h1 {
margin-bottom: 50px;
}
#box {
/* width: 50px; */
height: 50px;
line-height: 50px;
background: #efefef;
text-align: center;
margin-top: 40px;
box-shadow: 1px 1px 5px #000;
font-size: 30px;
}
.btn {
display: flex;
}
.inc,
.dec {
margin: 10px;
padding: 10px;
background: #fcfbfc;
margin-top: 30px;
text-transform: uppercase;
cursor: pointer;
}
.reset {
background: orange;
padding: 5px;
margin-top: 10px;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="counter.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<title>Counter</title>
</head>
<body>
<div class="main">
<h1 style="font-family: sans-serif;">Counter App</h1>
<div id="box">
</div>
<div class="btn">
<div class="inc" id="inc" onclick="inc();">
Increase
</div>
<div class="dec" id="dec" onclick="dec();">
Decrease
</div>
</div>
<div class="reset" onclick="reset();">
RESET
</div>
</div>
</body>
</html>
Now i have write a new script for it. Check it out all!
var a = 0;
var displayValue = document.getElementById('box');
var updateValue = function() {
displayValue.innerHTML = a;
};
var add = function(valueToAdd) {
a += valueToAdd;
updateValue();
};
var sub = function(valueToAdd) {
a -= valueToAdd;
updateValue();
};
var reset = function() {
a = 0;
updateValue();
};
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
body {
height: 100vh;
}
.main {
background: #efefef;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
padding: 30px;
}
.main h1 {
margin-bottom: 50px;
}
#box {
height: 50px;
line-height: 50px;
background: #efefef;
text-align: center;
margin-top: 40px;
box-shadow: 1px 1px 5px #000;
font-size: 30px;
}
.btn {
display: flex;
}
.inc,
.dec {
margin: 10px;
padding: 10px;
background: #fcfbfc;
margin-top: 30px;
text-transform: uppercase;
cursor: pointer;
}
.reset {
background: orange;
padding: 5px;
margin-top: 10px;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="counter.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<title>Counter</title>
</head>
<body>
<div class="main">
<h1 style="font-family: sans-serif;">Counter App</h1>
<div id="box">
</div>
<div class="btn">
<div class="inc" id="inc" onclick="add(1);">
Increment
</div>
<div class="dec" id="dec" onclick="sub(1);">
Decrement
</div>
</div>
<div class="reset" onclick="reset();">
RESET
</div>
</div>
</body>
</html>
I'm trying to make a text editor that can bold, italic and underline text using jQuery without using any HTML
The problem is that I can't make a selected text bold, the console log outputs "execCommand is not a function", am I doing something wrong? How can I achieve what I'm trying to do?
Here is my code:
(function ($) {
$.fn.text_editor = function(options) {
this.each(function() {
var buttons = {
buttons: ['bold', 'italic', 'underline']
};
var parametres = $.extend(buttons, options);
// generated html
$("body").html("<div class=\"container\">");
$("body").append("<h1 style=\"padding-left:55px;\">text editor</h1>");
$("body").append("<textarea rows=\"10\" cols=\"50\"></textarea>");
$("body").append("<br>");
$("body").append("<div class=\"buttons\"");
$("body").append("<button id=\"bold\">gras</button>");
$("body").append("<button id=\"italic\">italic</button>");
$("body").append("<button id=\"barre\">underline</button>");
// js
$("bold").execCommand("bold");
$("italic").execCommand("italic");
$("underline").execCommand("underline");
console.log($("bold"));
});
};
})(jQuery);
$(document).ready(function() {
$("textarea").text_editor()
});
After some searching, I came acrossthis article.
and this code pen. https://codepen.io/souporserious/pen/xBpEj
hope this helps.
$('.wysiwyg-controls a').on('click', function(e) {
e.preventDefault();
document.execCommand($(this).data('role'), false);
});
$controls-color: #ADB5B9;
$border-color: #C2CACF;
* {
box-sizing: border-box;
}
.wysiwyg-editor {
display: block;
width: 400px;
margin: 100px auto 0;
}
.wysiwyg-controls {
display: block;
width: 100%;
height: 35px;
border: 1px solid $border-color;
border-bottom: none;
border-radius: 3px 3px 0 0;
background: #fff;
a {
display: inline-block;
width: 35px;
height: 35px;
vertical-align: top;
line-height: 38px;
text-decoration: none;
text-align: center;
cursor: pointer;
color: $controls-color;
}
[data-role="bold"] {
font-weight: bold;
}
[data-role="italic"] {
font-style: italic;
}
[data-role="underline"] {
text-decoration: underline;
}
}
[class^="menu"] {
position: relative;
top: 48%;
display: block;
width: 65%;
height: 2px;
margin: 0 auto;
background: $controls-color;
&:before {
#extend [class^="menu"];
content: '';
top: -5px;
width: 80%;
}
&:after {
#extend [class^="menu"];
content: '';
top: 3px;
width: 80%;
}
}
.menu-left {
&:before, &:after {
margin-right: 4px;
}
}
.menu-right {
&:before, &:after {
margin-left: 4px;
}
}
.wysiwyg-content {
max-width: 100%;
width: 100%;
height: auto;
padding: 12px;
resize: both;
overflow: auto;
font-family: Helvetica, sans-serif;
font-size: 12px;
border: 1px solid $border-color;
border-radius: 0 0 3px 3px;
background: #F2F4F6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wysiwyg-editor">
<div class="wysiwyg-controls">
<a href='#' data-role='bold'>B</a>
<a href='#' data-role='italic'>I</a>
<a href='#' data-role='underline'>U</a>
</i>
</i>
</i>
</div>
<div class="wysiwyg-content" contenteditable>
<b>Let's make a statement!</b>
<br>
<i>This is an italicised sentence.</i>
<br>
<u>Very important information.</u>
</div>
</div>
NOTE: this is not my code.
execCommand is an experimental function, and its an function of document and not html elements. You can however call this function as below -
$("#bold").document.execCommand("bold");
$("#italic").document.execCommand("italic");
$("#underline").document.execCommand("underline");
I am creating a simple product list. I want to implement the editing of a product.
When I double-click on the product, I will be able to edit it. But i dont understand how i can do this. I hope, that you understand me because english is not my primary language. My code is here:
<div class="container">
<h1>Product List</h1>
<input type="text" name="newProduct" id="newProduct" placeholder="Enter your product here"/>
<ul id="productList"></ul>
<input type="checkbox" id="selectAll"/><label for="Select all">Select all</label>
<button id="deleteDoneProducts">Delete Selected</button>
</div>
CSS
* {
box-sizing:border-box;
}
body {
font-family: Tahoma, sans-serif;
}
.container {
margin:0 auto;
width:600px;
}
h1, #newProduct {
text-align: center;
width:598px;
}
#newProduct {
border:1px solid #999;
padding: 20px;
font-size: 28px;
box-shadow: 0px 0px 3px #888;
}
#productList {
list-style: none;
padding-left:0;
background-color: #F2F2F2;
}
.product {
padding: 15px 0px 15px 40px;
margin: 10px;
position: relative;
font-size: 24px;
box-shadow: 2px 2px 3px #848484;
background-color: #fff;
cursor: pointer;
text-transform: capitalize;
color:#000;
overflow: hidden;
}
.product:hover {
background-color: #F2F2F2;
}
.doneProduct {
margin-right: 40px;
}
.remove {
background-image: url(ico/delete_ico.png);
background-position: 0 0;
width:30px;
height: 30px;
position: absolute;
right: 20px;
top:13px;
display: none;
}
.remove:hover {
background-position: -34px 0px;
}
.product:hover .remove {
display: block;
}
#deleteDoneProducts {
float: right;
background-color: #a3d5df;
color: #fff;
padding: 10px;
border: none;
text-transform: uppercase;
font-weight: 900;
}
#deleteDoneProducts:hover {
background-color: #5fb5c7;
cursor: pointer;
}
jquery
function addNewProduct(e) {
if(e.keyCode == 13) {
var toAdd = $('input[name=newProduct]').val();
$('#productList').append('<li class="product"> <input type="checkbox" class="doneProduct"/>'+toAdd+'<div class="remove"></div><li/>');
$('#newProduct').val('');
e.preventDefault();
}
};
function deleteProduct() {
$(this).parent().remove();
};
function productDone() {
if (!$(this).is(":checked")){
$(this).parent().css({'textDecoration': 'none', 'color': '#000'})
} else {
$(this).parent().css({'textDecoration': 'line-through', 'color': '#999'});
};
};
function deleteAllSelected() {
$(".doneProduct:checked").parent().remove();
$('input[type="checkbox"]').removeAttr("checked");
};
function selectAllProducts() {
if (!$(".doneProduct").is(":checked")) {
$(".doneProduct").prop('checked', this.checked);
$(".doneProduct").parent().css({'textDecoration': 'line-through', 'color': '#999'});
} else {
$(".doneProduct").parent().css({'textDecoration': 'none', 'color': '#000'});
$(".doneProduct").prop('checked', this.checked);
}
};
$(function() {
$("#newProduct").on('keypress', addNewProduct);
$(document).on('click', ".remove", deleteProduct);
$(document).on('change', ".doneProduct", productDone);
$("#deleteDoneProducts").on('click', deleteAllSelected);
$("#selectAll").on('click', selectAllProducts);
$(".product").on('dbclick', editProductName);
})
https://jsfiddle.net/qp3nnfc5/5/ - this is fiddle)
Try this fiddle:
Change your addnewPRoduct function to:
function addNewProduct(e) {
if(e.keyCode == 13) {
var toAdd = $('input[name=newProduct]').val();
$('#productList').append('<li class="product"> <input type="checkbox" class="doneProduct"/><span>'+toAdd+'</span><div class="remove"></div><li/>');
$('#newProduct').val('');
e.preventDefault();
};
};
and Add this to your JS:
$("#productList").on("dblclick","li",function(){
$(this).find('span').attr("contentEditable",true)
})
Update :
Updated fiddle
Add this to JS
$("#productList").on("keypress","li",function(e){
if(e.which == 13){
e.preventDefault()
$(this).find('span').attr("contenteditable",false)
return;
}
})