Make selected text bold/unbold with jQuery - javascript

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");

Related

Get substring from url and copy to clip board?

Hey i am trying some thing for my school project, i hope you can help me in it.
I want to copy the substring from url, like
Url = https://www.example.com/blah/blah&code=12432
substring = 12432
Also i want to print the substring in Copy Box .
Please Help Me with this issue. It is required for my project to copy some text from string.
(function() {
var copyButton = document.querySelector('.copy button');
var copyInput = document.querySelector('.copy input');
copyButton.addEventListener('click', function(e) {
e.preventDefault();
var text = copyInput.select();
document.execCommand('copy');
});
copyInput.addEventListener('click', function() {
this.select();
});
})();
html, body {
height: 100%;
}
body {
font-size: 16px;
background: #FFD1DD;
display: flex;
align-items: center;
justify-content: center;
}
* {
box-sizing: border-box;
}
.wrapper {
padding: 0 5px;
}
h1 {
text-align: center;
font-size: 40px;
margin-bottom: 1.2em;
text-decoration: underline;
text-transform: uppercase;
}
p {
font-family: 'VT323', monospace;
font-size: 20px;
}
.container {
display: flex;
background: #FFA3BB;
border-radius: 7px;
padding: 10px;
margin: 0 auto;
}
h3 {
font-size: 28px;
text-transform: uppercase;
text-align: center;
span {
display: inline-block;
position: relative;
}
}
.copy, .paste {
flex-grow: 1;
width: 50%;
}
.copy {
border-right: 2px solid;
padding-right: 10px;
h3 {
span {
background: #76ECFF;
}
}
input {
padding-right: 90px;
}
}
.paste {
padding-left: 10px;
h3 {
span {
background: #FAE916;
}
}
}
form {
position: relative;
width: 100%;
input {
display: block;
width: 100%;
border: 3px solid;
outline: 0;
background: #FFF;
font-size: 25px;
padding: 5px 4px;
margin-bottom: 20px;
}
button {
display: block;
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
border: 0;
outline: 0;
color: #FFF;
background: #000;
font-family: 'VT323', monospace;
font-size: 25px;
text-transform: uppercase;
padding: 0.08em 0.8em;
cursor: pointer;
}
}
<div class="wrapper">
<h1>Link Copy</h1>
<p>Select the link text by clicking within the input then copy yourself or just click the copy button. Paste into the paste side to see that it works!</p>
<div class="container">
<div class="copy">
<h3>Copy <span><i class="fa fa-hand-peace-o"></i></span></h3>
<form>
<input type="text" value="https://codepen.io/she_codes/pen/OgrJJe/">
<button type="button">Copy</button>
</form>
</div>
<div class="paste">
<h3>Paste <span><i class="fa fa-smile-o"></i></span></h3>
<form>
<input type="text">
</form>
</div>
</div><!-- end .container -->
</div><!-- end .wrapper -->
Use this it will work
const params = new Proxy(new URLSearchParams(window.location.search), {
get: (searchParams, prop) => searchParams.get(prop),
});
// Get the value of "some_key" in eg "https://example.com/?some_key=some_value"
let value = params.some_key; // "some_value"
try this, assuming nothing is ever after "code="
var url = window.location.href;
var index = url.indexOf("code=");
var substring = url.substring(index + 5);
copyInput.setAttribute('value', substring);

Button not working in certain parts of my HTML

I have a button id="getstarted" located near the top and the bottom of my HTML. The button near the top works, but the one near the bottom does not work. They are formatted exactly the same. I want both buttons to work.
Here is my code.
**edited code. I added onclick="window.location.href = 'getstarted.html';" to my buttons. now the first button works, but the second one does not work. how do I get the second button to work?
$(document).ready(function() {
$(window).scroll(function() {
if ($(this).scrollTop() > 70) {
$('#header').fadeIn(500);
} else {
$('#header').fadeOut(500);
}
});
});
$(document).ready(function() {
$(window).scroll(function() {
if ($(this).scrollTop() > 70) {
$('#pricing').css("color", "#000");
} else {
$('#pricing').css("color", "#FFF");
}
});
});
// slideshow
$(document).ready(function() {
$("#laptopslideshop > div:gt(0)").hide();
setInterval(function() {
$('#laptopslideshop > div:first')
.fadeOut(500)
.next()
.fadeIn(500)
.end()
.appendTo('#laptopslideshop');
}, 3000);
});
#import url('https://fonts.googleapis.com/css?family=Nunito+Sans');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
#header {
position: fixed;
top: 0px;
width: 100%;
height: 84px;
background-color: #FFFFFF;
color: #000;
z-index: 1;
display: none;
}
#media (max-width: 768px) {
#header {
height: 60px;
}
}
#main {
height: 100vh;
width: 100%;
background: url(Images/teamchat.jpg) no-repeat center;
background-size: contain;
background-size: cover;
}
.headerContents {
text-align: right;
height: 100%;
width: 100%;
float: right;
z-index: 2;
position: fixed;
}
.headerpandc {
margin-right: 18%;
margin-top: 17px;
position: relative;
}
#media (max-width: 768px) {
.headerpandc {
margin-right: 4%;
margin-top: 14px;
}
}
#pricing {
font-family: Nunito Sans;
margin-right: 55px;
font-weight: 800;
letter-spacing: 0.1em;
font-size: 18px;
text-decoration: none;
color: #FFF;
transition: 0.6s;
}
#media (max-width: 768px) {
#pricing {
font-size: 12px;
margin-right: 3%;
}
}
#media (max-width: 355px) {
#pricing {
display: none;
}
}
#pricing:hover {
text-decoration: underline;
}
#pricing:active {
color: #000;
}
#getstarted {
font-family: Nunito Sans;
background-color: #5a52ff;
border-radius: 10px;
border: none;
color: white;
padding: 11px 25px;
text-align: center;
font-size: 18px;
transition: 0.3s;
text-decoration: none;
cursor: pointer;
font-weight: 800;
letter-spacing: 0.05em;
outline: none;
}
#media (max-width: 768px) {
#getstarted {
padding: 8px 15px;
font-size: 12px;
}
}
#getstarted:hover {
background-color: #3d33ff;
}
#getstarted:active {
transform: translateY(2px);
box-shadow: 0 1px #666;
}
#telosdesignlogo {
float: left;
height: 30px;
margin-left: 18%;
margin-top: 6px;
}
#media (max-width: 768px) {
#telosdesignlogo {
margin-left: 4%;
height: 23px;
}
}
/*main text*/
#mainbox {
height: 470px;
width: 500px;
text-align: left;
position: relative;
top: 250px;
left: 28%;
font-family: Nunito Sans;
color: #FFF;
}
#hitelos {
font-size: 2.5em;
font-weight: lighter;
}
#maintext {
font-weight: 800;
font-size: 3.5em;
}
#mainpricing {
z-index: 5;
}
/* Laptop slideshow begins */
#laptopslideshop {
position: relative;
margin-top: 50px;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}
.mySlides {
position: absolute;
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div id="header"></div>
<div class="headerContents">
<div class="headerpandc">
<a id="pricing" href="pricing.html">Pricing</a><button id="getstarted" onclick="window.location.href = 'getstarted.html';">Get Started</button>
<img src="Images/TelosLogo with text.png" href="index.html" alt="TelosDesign" id="telosdesignlogo" />
</div>
</div>
<div id="main">
<div id="mainbox">
<div id="hitelos">
Hi! We're Telos.
</div>
<div id="maintext">
Beautiful websites tailored to your unique business.
</div>
<button id="getstarted" onclick="window.location.href = 'getstarted.html';">Get Started</button>
</div>
</div>
<div id="laptopslideshop">
<div>
<img class="mySlides" src="Images/laptoppic1test.png" alt="Laptop and Phone" />
</div>
<div>
<img class="mySlides" src="Images/laptoppic2test.png" alt="Laptop and Phone" />
</div>
</div>
id should be unique to an element(just one), use a class instead
In your code you use javascript(jquery) to control href, not html, see onclick.
<button id="getstarted" onclick="window.location.href = 'getstarted.html';">
The javascript is taking in consideration only the first element because no other element with the same id should exist.
--
I see that you have also:
<script type="text/javascript" src="app.js"></script>
What this code is doing ?
Check the web browser console for JavaScript errors from other sources/code.
Try with this code
<a id="pricing" href="http://google.com">Pricing</a>
<button id="getstarted">Get Started</button>
<a href="index.html">
<img src="Images/TelosLogo with text.png" href="http://google.com" alt="TelosDesign" id="telosdesignlogo"></a>

How to prepend a li to the top of another li

I am creating a list of elements from user inputs. Each element in this list comes with a dynamically created button within my JavaScript.
I am trying to have it so that when I click on this dynamically created button ("started"), it will automatically remove the entire list element from the original li and move it to the top of another li.
My code: [EDIT: For some reason, my code snippet doesn't show the second list "doing" but it shows in my local console, not sure if it's problem with my code?]
// Dynamically creates a new li element with 'started' button after user puts in text and clicks a button similar to 'submit'
$(document).ready(
$("#new-item").on('click', function() {
// once the document loads, create new item with this function
var text = $('#task').val();
if (text != '') {
$('#todo-list').prepend("<li class='addedTask'> <button id='started'>Started</button>" + text + '</li>' + '</br>');
}
})
);
$(".addedTask").on('click', "button", function() {
var completedItem = $(this).parent();
$('#doing-list').append($('#todo-list'( completedItem)).removeClass(completedItem));
});
header {
background-color: #026aa7;
height: 30px;
text-align: center;
padding: 5px 8px;
}
header a {
height: 30px;
width: 80px;
text-align: center;
background-image: url(https://a.trellocdn.com/dist/images/header-logo-2x.01ef898811a879595cea.png);
background-repeat: no-repeat;
text-align: center;
display: inline-block;
background-size: 80px 30px;
}
body {
background-color: #0078c0;
margin: 0px;
font-family: sans-serif;
}
li {
list-style-type: none;
margin-left: 8px;
text-indent: 10px;
/* cursor: pointer;*/
}
li:hover {
background: #ddd;
}
li.addedTask {
border: 1px solid white;
border-radius: 4px;
padding: 15px;
width: 83%;
background-color: white;
}
#started {
float: left;
background-color: white;
border-radius: 6px;
}
.column {
background-color: #E3E3E3;
min-height: 100px;
width: 25%;
box-shadow: 1px 1px 3px $shadow;
display: inline-block;
position: asolute;
margin: 5px;
vertical-align: top;
position: relative;
top: 75px;
right: 287px;
border-radius: 5px;
}
.column h1 {
font-size: 20px;
padding-left: 10px;
position: relative;
bottom: .5px;
color: #393939;
margin: 5px;
}
#new-item {
position: relative;
left: 40px;
top: 20px;
font-family: sans-serif;
padding: 4px;
width: 100px;
background-color: #ffffff;
border-radius: 4px;
}
#task {
position: relative;
left: 25px;
top: 20px;
height: 20px;
width: 200px;
padding: 10px;
border-radius: 4px;
padding-left: 4px;
}
<html>
<head>
<title> HW03 Javascript and jQuery </title>
<link rel="stylesheet" href="_css/style.css">
</head>
<body>
<header>
</header>
<section>
<!-- create input tag for user input -->
<input type="text" id="task">
<!-- button takes input and adds a new element with content to 'list_do' -->
<button id="new-item"> Add a card </button>
<!-- ability to move items between list_todo and list_doing -->
<div class="column" id="to-do">
<h1> To Do </h1>
<li id="todo-list"></li>
</div>
<div class="column" id="doing">
<h1> Doing </h1>
<li id="doing-list"></li>
</div>
</section>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="_js/main.js">
</script>
</body>
</html>
Here is an image of what it looks like on my end:
You're using the wrong selector .addedTask for the Event delegation.
Use prepend function to add the element at the top.
To test this snippet, click on Full Page after you click 'Run code snippet'
// Dynamically creates a new li element with 'started' button after user puts in text and clicks a button similar to 'submit'
$(document).ready(
$("#new-item").on('click', function() {
// once the document loads, create new item with this function
var text = $('#task').val();
if (text != '') {
$('#todo-list').prepend("<li class='addedTask'> <button id='started'>Started</button>" + text + '</li>' + '</br>');
}
})
);
$("#todo-list").on('click', "button", function() {
var completedItem = $(this).parent();
$('#doing-list').prepend($(completedItem));
});
header {
background-color: #026aa7;
height: 30px;
text-align: center;
padding: 5px 8px;
}
header a {
height: 30px;
width: 80px;
text-align: center;
background-image: url(https://a.trellocdn.com/dist/images/header-logo-2x.01ef898811a879595cea.png);
background-repeat: no-repeat;
text-align: center;
display: inline-block;
background-size: 80px 30px;
}
body {
background-color: #0078c0;
margin: 0px;
font-family: sans-serif;
}
li {
list-style-type: none;
margin-left: 8px;
text-indent: 10px;
/* cursor: pointer;*/
}
li:hover {
background: #ddd;
}
li.addedTask {
border: 1px solid white;
border-radius: 4px;
padding: 15px;
width: 83%;
background-color: white;
}
#started {
float: left;
background-color: white;
border-radius: 6px;
}
.column {
background-color: #E3E3E3;
min-height: 100px;
width: 25%;
box-shadow: 1px 1px 3px $shadow;
display: inline-block;
position: asolute;
margin: 5px;
vertical-align: top;
position: relative;
top: 75px;
right: 287px;
border-radius: 5px;
}
.column h1 {
font-size: 20px;
padding-left: 10px;
position: relative;
bottom: .5px;
color: #393939;
margin: 5px;
}
#new-item {
position: relative;
left: 40px;
top: 20px;
font-family: sans-serif;
padding: 4px;
width: 100px;
background-color: #ffffff;
border-radius: 4px;
}
#task {
position: relative;
left: 25px;
top: 20px;
height: 20px;
width: 200px;
padding: 10px;
border-radius: 4px;
padding-left: 4px;
}
<html>
<head>
<title> HW03 Javascript and jQuery </title>
<link rel="stylesheet" href="_css/style.css">
</head>
<body>
<header>
</header>
<section>
<!-- create input tag for user input -->
<input type="text" id="task">
<!-- button takes input and adds a new element with content to 'list_do' -->
<button id="new-item"> Add a card </button>
<!-- ability to move items between list_todo and list_doing -->
<div class="column" id="to-do">
<h1> To Do </h1>
<li id="todo-list"></li>
</div>
<div class="column" id="doing">
<h1> Doing </h1>
<li id="doing-list"></li>
</div>
</section>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="_js/main.js">
</script>
</body>
</html>
Since you are adding HTML to the DOM later, the code $(".addedTask").on('click', "button", function() { will not work because at the time of execution this code there was no .addedTask elements.
You may instead want to delegate this to a higher node in the DOM. One easy way to accomplish this would be to rewrite this as
$("document").on('click', ".addedTask button", function() {

dynamic html element issue

when one of menus is chosen and option in select tag, apply button can be worked to be placed to be dynamic html element in Panel below. when a user clicks cancel button, that dynamic html element will be deleted.
As for an issue, when a user chooses one menu, dynamic html element will be redundantly placed to be in panels.
For example,
apply menu 2 > create dynamic html element in panel of menu2 > cancel menu 2 > apply menu other menu (3 or 1) > redundantly create dynamic html elements in previous panel of menu 2 and current panel of menu.
How am I able to completely delete dynamic html element when I click cancel button??
$(function() {
$(".section4 ul li:first-child").addClass("on");
//section4 ul li on
$("section.section4 ul li").click(function() {
$(this).addClass("on");
$("section.section4 ul li.on").not(this).removeClass("on");
});
// panel
$(".PaNel").hide();
$(".PaNel:eq(0)").show();
//addEventListner
$(".section4 ul li").click(function() {
$(".PaNel").hide();
$("#tab" + ($(this).index() + 1)).show();
});
//메뉴 선택
$(".section2").find("article").click(function() {
$(this).addClass("On");
$("article.On").not(this).removeClass("On");
});
//비활성신청
$(".btn2").css({
"display": "none"
});
//상단 메뉴
$("article").click(function() {
if ($(this).hasClass("On") && $("#menuSelect option:selected").index() > 0) {
$(".btn1").css({
"background": "red"
});
//$(".btn2").css({"display":"block"});
} else {
$(".btn1").css({
"display": "block"
});
$(".btn2").css({
"display": "none"
});
}
});
// while article is clicked, menuSelect.index() > 0
$("body").click(function() {
if ($("article").hasClass("On")) {
if ($("#menuSelect option:selected").index() > 0) {
$(".btn1").css({
"display": "none"
});
$(".btn2").css({
"display": "block"
});
}
}
})
$(".area_popup").addClass("none")
$(".end").addClass("none");
$(".section2").children("article").one("click", function() {
console.log($(this).index())
var target = $("#tab" + $(this).index())
$("#Apply").click(function() {
var menuSelect = document.getElementById("menuSelect");
//console.log(menuSelect);
switch (menuSelect.value) {
case "a":
case "b":
case "c":
case "d":
target.find(".Apply_Check").append("<div class='User'>" + menuSelect.value + "</div>");
break;
}
$(".end").removeClass("none");
$("#Apply").addClass("none");
//alert("신청 완료 되었습니다")
})
})
$(document).on("click", '.end', function() {
//본인꺼만
$(".User").remove();
$(".end").addClass("none");
$("#Apply").removeClass("none");
});
$("article").click(function() {
if (parseInt($(this).find("span").text()) == 0) {
//$(".button").css({"display":"none"});
//$(".btn1,.btn2, .end").css({"display":"none"})
//$(".btn4").css({"display":"block"});
$(".area_popup3").css({
"display": "block"
});
} else {
//$(".button").css({"display":"block"});
//$(".btn4").css({"display":"none"});
}
});
//메뉴 하$(단 클릭시 섹션 2 남은 수량이 없는 경우
$(".section4 ul").children("li").click(function() {
var article = $('article:eq(' + $(this).index() + ')')
if (parseInt(article.find("span").text()) == 0) {
$(".area_popup3").css({
"display": "block"
});
}
})
$("body").click(function(e) {
if ($("#menuSelect option:selected").index() == 0) {
$(".btn1").css({
"display": "block"
});
$(".btn2").css({
"display": "none"
});
}
})
});
function fn_popup_close(name) {
//$('body').removeClass('fixed');
//body class removeClass
$('.' + name).hide();
}
function fn_popup_open(name) {
//$('body').addClass('fixed');
$('.' + name).show();
}
/*//////////////////reset//////////////////////////////////////*/
* {
margin: 0;
padding: 0;
}
body,
header,
footer,
section,
nav,
article,
figure,
aside,
details,
main {
margin: 0;
padding: 0;
}
header,
footer,
section,
nav,
article,
figure,
aside,
details,
main {
display: block;
}
a:link,
a:visited {
color: #000;
text-decoration: none;
}
/*a:hover, a:focus{color:#aaa; text-decoration:none;}*/
body {
color: #333;
}
li {
list-style: none;
}
input[type="button"] {
cursor: pointer;
}
input[type=button],
select {
border-radius: 0;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
/*//////////////////reset//////////////////////////////////////*/
#wrap {
max-width: 100%;
margin: 0 auto;
}
/*//////////////section1/////////////////////////////////////*/
section.section1 {
width: 100%;
/*background:#F87141;*/
}
section.section1 .screen_info {
width: 100%;
overflow: hidden;
}
section.section1 .screen_info ul {
width: 300%;
overflow: hidden;
}
section.section1 .screen_info ul li {
width: 31.63%;
float: left;
padding: 10px 1% 10px .7%;
}
/*//////////////section2/////////////////////////////////////*/
section.section2 {
width: 100%;
overflow: hidden;
border: 1px solid #ccc;
border-width: 1px 0;
}
section.section2 h3 {
width: 100%;
height: 50px;
line-height: 50px;
border-bottom: 1px solid #ccc;
text-indent: 2%;
}
section.section2 article {
width: 31.33%;
padding-left: 2%;
height: 100px;
float: left;
}
section.section2 article div {
border-right: 1px solid #ccc;
}
.On {
background: #d4dbdd;
}
/*section.section2 article:last-child{border-right:none;}*/
section.section2 article h2 {
width: 100%;
height: 30px;
font-size: 14px;
line-height: 30px;
}
section.section2 article p.FoodName {
width: 100%;
height: 40px;
line-height: 40px;
text-align: center;
font-weight: 900;
font-size: 20px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
section.section2 article p.FoodCnt {
width: 90%;
height: 30px;
line-height: 30px;
text-align: right;
font-size: 13px;
}
/*//////////////section2/////////////////////////////////////*/
/*//////////////section3/////////////////////////////////////*/
section.section3 {
width: 100%;
padding-top: 30px;
}
section.section3 .Select {
border-top: 1px solid #ccc;
position: relative;
}
.Select {
display: block;
content: "";
clear: both;
}
section.section3 .Select p.Selected {
height: 40px;
line-height: 40px;
border-bottom: 1px solid #ccc;
}
section.section3 .Select p {
font-weight: 900;
text-indent: 9%;
}
section.section3 .Select p.Selected span {
cursor: pointer;
position: absolute;
right: 20px;
top: 4px;
}
section.section3 .Select p.Selected span img {
width: 10px;
}
.selection {
border-bottom: 1px solid #ccc;
}
/*.on{background:#ccc;}*/
section.section3 select {
width: 100%;
height: 30px;
background: #ECEFF0;
border: 1px solid #ccc;
border-width: 1px 0;
}
select#menuSelect::-ms-expand {
display: none;
}
/*IE*/
select#menuSelect {
appearance: none;
-webkit-appearance: none;
/*for chrome*/
-moz-appearance: none;
/*for firefox*/
background: url(./images/next_shadow.png) no-repeat right;
background-position-x: 97%;
background-size: 14px 24px;
text-indent: 2%;
}
section.section3 p.Avail_time {
width: 95%;
text-align: right;
line-height: 30px;
height: 30px;
padding-right: 5%;
}
section.section3 p input[type=button] {
width: 100%;
border: 1px solid #ccc;
border-width: 1px 0;
background: #F87141;
height: 40px;
color: #fff;
}
section.section3 p.btn1 input[type=button] {
background: #aaa;
}
section.section3 p.end input[type=button] {
background: #aaa;
}
.none {
display: none;
}
/*//////////////section3/////////////////////////////////////*/
/*//////////////section4/////////////////////////////////////*/
section.section4 {
padding-top: 30px;
}
section.section4 ul {
width: 100%;
overflow: hidden;
border: 1px solid #ccc;
border-width: 1px 0;
}
section.section4 ul li {
width: 25%;
height: 30px;
line-height: 30px;
float: left;
font-weight: 900;
font-size: 13px;
text-align: center;
background: #fff;
}
section.section4 ul li a {
display: block;
border-right: 1px solid #ccc;
}
.on>a {
background: #aaa;
color: #fff;
}
section.section4 ul li:last-child a {
border: none;
}
section.section4 .memo {
width: 100%;
}
section.section4 .memo input {
width: 100%;
}
section.section4 article.PaNel {
width: 98%;
padding-left: 2%;
height: 400px;
}
.Apply_Check {
width: 100%;
}
.Apply_Check p.Count {
width: 100%;
height: 50px;
line-height: 50px;
font-weight: 900;
}
#tab4 {
padding: 10px;
}
#tab4 p {
padding-bottom: 20px;
}
/*//////////////dynamic HTML Element ////////////////////////////////////*/
.User {
width: 100%;
height: 50px;
background: #aaa;
}
/*//////////////dynamic HTML Element ////////////////////////////////////*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<section class="section2">
<h3>choose menu</h3>
<article>
<div>
<h2>menu 1.</h2>
</div>
</article>
<article class="scene_two">
<div>
<h2>menu 2.</h2>
</div>
</article>
<article class="scene_three">
<div>
<h2>menu 3.</h2>
</div>
</article>
</section>
<section class="section3">
<select id="menuSelect">
<option value="menu" selected="selected">choose one</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
<!--<div class="Select">
<p class="Selected" id="reasonSelect" onclick="result();">신청사유 선택<span><img src="./images/next_shadow.png" alt="arrow"/></span></p>
<div class="selection">
<p>외근/출장</p>
<p>당직</p>
<p>당직</p>
<p>기타</p>
</div>
</div>-->
<p class="btn1 button"><input type="button" value="apply" /></p>
<p class="btn2 button"><input type="button" value="apply" id="Apply" /></p>
<p class="end button"><input type="button" value="cancel" onclick="fn_popup_open('area_popup')" /></p>
<!--<p class="btn4 button"><input type="button" value="신청 마감" onclick="fn_popup_open('area_popup')"/></p>-->
<!--<select>
<option>dd</option>
<option>dd</option>
<option>dd</option>
<option>ddd</option>
</select>-->
</section>
</section>
<section class="section4">
<ul>
<li>menu1</li>
<li class="scene_two">menu2</li>
<li class="scene_three">menu3</li>
<li>info</li>
</ul>
<article class="PaNel" id="tab1">
<div class="Apply_Check">
<p class="Count">Panel <span id="ppl1">1</span></p>
</div>
</article>
<article class="PaNel" id="tab2">
<div class="Apply_Check">
<p class="Count">Panel <span id="ppl2">2</span></p>
</div>
</article>
<article class="PaNel" id="tab3">
<div class="Apply_Check">
<p class="Count">Panel <span id="ppl3">3</span></p>
</div>
</article>
<article class="PaNel" id="tab4">
information
</article>
</section>
</div>
You could use JQuery's empty command, it removes all child elements from the parent element
$("#parent").empty()

Why is my page directing back to the login page when I press enter to add a comment

I have created an Instagram clone and all of my functionality (Like photos, add comments) is working fine when in jsbin: http://jsbin.com/tecaha/edit?js,output
For example, when I type a comment and press enter, the comment is added. However, when I copy this code into Atom and upload to my server, or use my local environment, when I press enter the page just loops back to the login.
Can someone tell me what is wrong here?
Here is the link to the page on the server http://jshuadvd.com/instagram/feed.html
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<header>
<div class="top">
<ul>
<li><i class="fa fa-home fa-2x"></i>
</li>
<li id="logo">
<img src="http://i.imgur.com/SmdPZ6T.png" />
</li>
<li id="profile-photo">
<img src="https://scontent-sea1-1.cdninstagram.com/hphotos-xaf1/t51.2885-19/10731946_1517235648523785_1216221661_a.jpg" />
<p class="username">username</p>
</li>
</ul>
</div>
</header>
<div id="container">
<main>
<div id="feed-container">
<ul id="images">
</ul>
</div>
</main>
</div>
</body>
</html>
CSS
* {
margin: 0px;
padding: 0px;
}
body {
font-family: "proxima-nova", "Helvetica Neue", Arial, Helvetica, sans-serif;
}
ul {
list-style: none;
}
li {
list-style: none;
}
#container {
margin: 0 auto;
display: block;
}
header {
margin: 0 auto;
display: block;
background-color: #3E6D93;
height: 50px;
}
.top {
background: #467ea6;
background: -webkit-gradient(linear, 0 0, 0 100%, color-stop(0.01, #467ea6), to(#27608c));
background: -webkit-linear-gradient(#467ea6 1%, #27608c 100%);
background: -moz-linear-gradient(#467ea6 1%, #27608c 100%);
background: -o-linear-gradient(#467ea6 1%, #27608c 100%);
background: linear-gradient(#467ea6 1%, #27608c 100%);
filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#467ea6', endColorstr='#27608c', GradientType=0);
width: 100%;
}
.top li {
display: inline-block;
margin: 0 auto;
}
#home-button, .fa-home {
float: left;
background-color: #305F87;
color: #ccc;
padding: 7px;
margin-left: 200px;
width: 35px;
}
.fa-home {
padding-left: 12px;
}
#logo img {
float: right;
width: 110px;
margin-left: 400px;
padding: 6px;
}
#profile-photo {
float: right;
margin-right: 200px;
padding: 10px;
border-left: 1px solid #305F87;
border-right: 1px solid #305F87;
width: auto;
}
#profile-photo img {
width: 30px;
height: 30px;
border-radius: 5px;
}
#profile-photo p {
color: #fff;
font-weight: bold;
font-size: 14px;
text-align: center;
float: right;
margin-left: 10px;
padding: 5px;
}
#feed-container {
background-color: #DFDFDF;
width: 650px;
height: auto;
margin: 0 auto;
display: block;
}
#images {
margin: 0 auto;
display: block;
padding-top: 50px;
padding-bottom: 30px;
}
#images img {
margin: 0 auto;
display: block;
width: 550px;
height: 550px;
}
.below-image {
margin: 0 auto;
display: block;
width: 550px;
height: 52px;
background-color: #fff;
border: 1px solid #ccc;
}
.like {
float: left;
}
.fa-heart {
color: #5a5a5a;
padding: 10px;
border-right: 1px solid #ccc;
}
.liked {
color: #ff0000;
}
.image-info {
float: left;
text-align: left;
width: 437px;
height: 52px;
border-right: 1px solid #ccc;
}
.image-info p {
color: #467ea6;
font-weight: bold;
padding-top: 15px;
padding-left: 20px;
float: left;
}
.more {
float: right;
color: #5a5a5a;
}
.more, .info {
display: inline-block;
font-size: 45px;
color: #5a5a5a;
padding: 0 5px;
background-color: transparent;
border: 0;
cursor: pointer;
outline: none;
}
.clear {
clear: both;
}
.comment-container {
width: 550px;
margin-bottom: 65px;
}
.add-a-comment {
margin: 0 auto;
display: block;
background-color: #fff;
width: 550px;
height: auto;
margin-bottom: 50px;
}
.add-a-comment p {
margin: 0 auto;
display: block;
}
.add-a-comment input[type=text] {
width: 420px;
border: 1px solid #ccc;
color: #4f4f4f;
font-size: 16px;
border: 0;
bottom: 40px;
padding: 15px;
outline: none;
background: transparent;
}
input, select, textarea{
color: #f00;
}
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0px 1000px white inset;
}
.inserted {
cursor: pointer;
}
.post-liked .fa-heart {
color: #f00;
}
.comment-click {
width: 30px;
text-align: center;
border: 1px solid #ccc;
color: #4f4f4f;
font-size: 16px;
border: 0;
bottom: 40px;
padding: 15px;
outline: none;
background: transparent;
cursor: pointer;
float: right;
}
.comment-list {
width: 400px;
text-align: left;
}
.comment-list li{
margin-left: 20px;
color: #4f4f4f;
font-weight: 600;
padding-top: 5px;
}
.fixed-container {
height: 250px;
width: 530px;
overflow: scroll;
}
.un {
color: #467ea6;
}
JavaScript / jQuery
// Variable of html strings
var bottom = '<div class="below-image">' +
'<div class="like">' +
'<i class="fa fa-heart fa-2x"></i>' +
'</div>' +
'<div class="image-info">' +
'<p>User Info</p>' +
'</div>' +
'<div class="more">' +
'<button class="info">···</button>' +
'</div>' +
'<div class="clear"></div>' +
'</div>' +
'<div class="add-a-comment">' +
'<div class="comment-container">' +
'<form class="comment" action="index.html" method="post">' +
'<input id="enter" type="text" name="newcomment" value="" autocomplete="off" placeholder="Add a comment...">' +
'<button class="comment-click"></button>' +
'</form>' +
'<ul class="comment-list fixed-container">' +
'</ul>' +
'</div>' +
'</div>' +
'</li>' +
'</div>' +
'</div>';
// GET Request to grab the data from the JSON file
$(document).ready(function() {
var jsonURL = "https://codesmith-precourse.firebaseio.com/instagram/-JqL35o8u6t3dTQaFXSV.json";
var newImage = "http://yourbizrules.com/wp-content/uploads/2014/08/Staying-Motivated.jpg";
$.getJSON(jsonURL, function(json) {
var imgList = "";
$.each(json, function (i) {
imgList += '<div class="post"><li><img class="inserted" src= " ' + json[i] + ' ">' + bottom;
});
$('#images').append(imgList);
// Like Photos
$('#images').on('click', '.inserted', function() {
// $('.fa-heart').toggleClass('liked');
var post_block = $(this).parents('.post').first();
post_block.toggleClass('post-liked');
});
// Add Comment
$('.comment-click').on('click', function() {
var userName = "Username";
//debugger;
//console.log("username", userName, $(userName));
var userComment = $(this).siblings('input[name=newcomment]').val();
$(this).parent().siblings('.comment-list').append("<li>" + userName + ' ' + userComment + "</li>");
$.each($('input'), function () {
$(this).val("");
});
});
});
});
The issue you have is that the form gets submitted when the user presses the enter key, so how to fix that is to hook the onSubmit event of the form.
To make this work, you need to apply these two changes:
1. convert your button to a submit button so that clicking the button will submit the form
<button class="comment-click" type="submit"></button>
2. Hook the on submit event, which will be called either when the user presses enter or the user clicks the submit button:
$('form.comment').on('submit', function(evt) {
var userName = "Username";
// note: "this" references the <form> element!
var userComment = $(this).find('input[name=newcomment]').val();
$(this).siblings('.comment-list').append("<li>" + userName + ' ' + userComment + "</li>");
$(this).find('input[name=newcomment]').val("");
// prevents the standard behaviour
evt.preventDefault();
return false;
});
3. remove your old code i.e the $('.comment-click').on('click', function() { ...
Also you should escape the user's input to prevent HTML/JS injection:
$(this).siblings('.comment-list').append($("<li></li>").text(userName + ' ' + userComment));
You can try this by entering something like <h1>big! on your current implementation.
because your comment form's action points to index.html
your index.html asks for login: http://jshuadvd.com/instagram/index.html
<form class="comment" action="index.html" method="post">
---
---
---
</form>

Categories