Transfer jsfiddle to dreamweaver - javascript

I give. I can't seem to get the jsfiddle code to work when I transfer it to dreamweaver? I must be missing something simple?? It works fine in jsfiddle. I added the ready function as opposed to the onload, but it still is not working.
$("document").ready(function() {
var h1 = $('div#greeting h1');
h1.hide().contents().each(function() {
var words;
if (this.nodeType === 3) {
words = this.data.split(/\s+/).map(function(w) {
return '<span>' + w + '</span>';
}).join('');
$(this).replaceWith(words);
} else if (this.nodeType === 1) {
$(this).html($(this).html().split(/\s+/).map(function(w) {
return '<span>' + w + '</span>';
}).join(''));
}
});
h1.find('span').hide().each(function() {
if (!$.trim(this.innerHTML)) {
$(this).remove();
}
});
$('#start').on('click', function() {
h1.show().find('span').each(function(i, e) {
h1.queue(function(next) {
$.when($(e).fadeIn(150).fadeOut(150)).done(next);
});
});
});
$('#stop').on('click', function() {
h1.finish();
});
$('#reset').on('click', function() {
h1.finish();
});
.box {
border: 1px solid #E38E34;
background-color: #FFE7BF;
height: 150px;
-webkit-border-radius: 1px;
-moz-border-radius: 1px;
border-radius: 1px;
}
#title {
margin: 5px;
border-bottom: 1px solid #E38E34;
color: #C46908;
font-weight: bold;
}
#message {
margin: 5px;
}
h1 {
font-size: 50px;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="styles.css">
<title>TedsCode</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="speedread.js"></script>
</head>
<body>
<div class="box" />
<div id="title"><span id="name">Title</span>
</div>
<div id="message" />
<div id="greeting" />
<input type="button" value="Read" id="start" />
<input type="button" value="Stop" id="stop" />
<input type="button" value="Reset" id="reset" />
<h1><center>This is a test to see if this script displays one word at a time and functions correctly.</center></h1>
</body>
</html>

You hava a syntax error in js.
I comment on something: add "});" to js, and remove redundant reference of jquery.
Code snippet works now

I can see two things:
1-Why are you using two version of jquery at the same time 1.9.1 and 2.1.0?
2-In the code you paste, the $("document").ready(function() {
isn't properly closed
Are you receiving any error in the console or the network ?

Related

Uncaught (in promise) JavaScript - Skulpt Input Function

Im trying to write a Python code editor in browser using Skulpt to execute the Python code and Code Mirror as a text editor.
As the input function for Skulpt uses an alert for input, I have decided to write my own function. The input function I have written works and takes an input from a modal.
Sk.configure({output:outf, read:builtinRead, __future__: Sk.python3, inputfun: function (prompt) {
clearModal();
addModalQuestion(prompt);
toggleModal();
// the function returns a promise to give a result back later...
return new Promise(function(resolve,reject){
$("#modal-submit").on("click",function(e){
// resolve the promise with the value of the input field
resolve($("#modal-input").val());
})
})
}, inputfunTakesPrompt: true});
Any errors in the code are usually sent to the output pre:
For normal errors with no input I get this (which is what I want to happen):
However, when I use an input in my code no error message is output:
The errors will no longer be displayed and I receive the following error:
Does anyone have any idea how I could resolve this issue or point me in the right direction?
I've put both my skulpt.js and index.html file below if this helps.
SkulptTest.js
// output functions are configurable. This one just appends some text
// to a pre element.
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
// Here's everything you need to run a python program in skulpt
// grab the code from your textarea
// get a reference to your pre element for output
// configure the output function
// call Sk.importMainWithBody()
function runit() {
var prog = editor.getValue(); ;
var mypre = document.getElementById("output");
mypre.innerHTML = '';
Sk.pre = "output";
Sk.configure({output:outf, read:builtinRead, __future__: Sk.python3, inputfun: function (prompt) {
clearModal();
addModalQuestion(prompt);
toggleModal();
// the function returns a promise to give a result back later...
return new Promise(function(resolve,reject){
$("#modal-submit").on("click",function(e){
// resolve the promise with the value of the input field
resolve($("#modal-input").val());
})
})
}, inputfunTakesPrompt: true});
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
var myPromise = Sk.misceval.asyncToPromise(function() {
try {
if (result = Sk.importMainWithBody("<stdin>", false, prog, true)) {
return result;
}
}
catch(e) {
outf(e.toString());
}
});
;
}
index.html
<!DOCTYPE html>
<html>
<head>
<!--JQuery-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<!--Skulpt-->
<script src="skulpt/skulpt.min.js" type="text/javascript"></script>
<script src="skulpt/skulpt-stdlib.js" type="text/javascript"></script>
<!--Code Mirror-->
<script src="codemirror/lib/codemirror.js"></script>
<link href="codemirror/lib/codemirror.css" rel="stylesheet">
<script src="codemirror/mode/python/python.js"></script>
<link href="codemirror/theme/midnight.css" rel="stylesheet">
<!--My Styles-->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data Input</title>
<meta name="description" content="How do we input data in Python 3?">
<link href="css/reset.css" rel="stylesheet" type="text/css">
<style>
#editor-container {
width: 100vw;
height: 47vh;
}
#run-container {
width: 100vw;
height: 5vh;
background-color: #0a121f;
}
#run-container button {
float: right;
height: 5vh;
width: 15%;
background-color: yellow;
border: none;
border-radius: 5px;
}
#output-container {
width: calc(100vw - 10px);
height: calc(45vh - 10px);
background-color: aliceblue;
float: right;
background-color: #000;
color: #fff;
padding-left: 10px;
padding-top: 10px;
}
#output {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
</style>
<!--Modal-->
<link href="css/modal.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--Skulp-->
<script src="skulptTest.js"></script>
<!--My Code-->
<div id="right-panel">
<div id="editor-container">
<div class="modal" id="modal">
<div class="modal-content">
<span class="close-button"></span>
<p id="modal-question" class="modal-question">Input Prompt</p>
<input type="text" id="modal-input" name="modal-input" class="modal-input">
<button id="modal-submit" class="modal-submit" onclick="toggleModal()">Submit</button>
</div>
</div>
<textarea id="editor"></textarea>
</div>
<div id="run-container">
<button type="button" onclick="runit()">Run</button>
</div>
</div>
<div id="output-container">
<pre id="output"></pre>
</div>
<!-- Code Mirror-->
<script src="js/code-mirror.js"></script>
<script>editor.setValue("name = input('What is your name?')\nprint(name)");</script>
</body>
</html>
<script src="js/modal.js"></script>
I've also attached a link to all of the files below.
Code Editor Files
If anyone could help me with this I'd really appreciate it
Thanks in advance,
Sean

Javascript and Jquery Quotes generator

Hey I am trying to generate a random quotes using Javascript and Jquery.
Can someone tell me why it isn't working? I wrote a random generate function inside the document.ready and using it to generate random array index, but it is not out putting any quotes. Please help
<!DOCTYPE html>
<html>
<head>
<title>Random Quotes</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div id="box">
<p id="quotesBox"></p>
<button type="button" id="quoteButton" class="buttons" class="btn btn-primary">Give me a Quote</button>
<button type="button" id="clearButton" class="buttons" class="btn btn-primary">Clear</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
$(document).ready(function(){
var randomQuotes = [
"Doh",
"Mom, where's my meatloaf",
"This is spartan",
"Stay here,I will be back ",
"You talking to me? You talking to me??"
];
$("#quoteButton").on('click', function(){
function randomQuotes() {
return Math.floor(Math.random() * randomQuotes.length);
}
$("#quotesBox").text(randomQuotes[randomQuotes()]);
});
$("#clearButton").on('click', function(){
$("#quotesBox").text("");
});
});
#box {
height: 500px;
width: 500px;
background: rgb(205, 255, 255);
margin: 0 auto;
}
.buttons {
position: 0px 0px 0px 70px;
margin: 100px -130px 0px 150px;
border: 2px solid;
border-radius: 25px;
color: #FF00FF;
background: white;
}
#quotesBox {
font-weight: 600;
text-align: center;
padding-top: 150px;
}
You need to make the following changes to your javascript:
http://codepen.io/anon/pen/GqpBzj
var randomQuotes = [
"doh",
"Mom, where's my meatloaf",
"This is spartan",
"Stay here,I will be back ",
"You talking to me? You talking to me??"
];
$("#quoteButton").on('click', function() {
var x = Math.floor(Math.random() * randomQuotes.length);
$("#quotesBox").text(randomQuotes[x]);
});
$("#clearButton").on('click', function() {
$("#quotesBox").text("");
});
-Reasons For Changes-
1) You don't need the $(document).ready(function(){}); if you are simply adding event handlers
2) Refrain from defining functions within other functions when it is not necessary. You can simplify that by just passing the Math.floor(...) to a variable.
3) Don't be afraid to write things out. No reason to mega-condense things all into one line.
4) Keep function/variable names unique or more descriptive. It is confusing for other users when they see an array and a function with very similar names.
The function and array's name seems the same, it works when one of them is changed
$(document).ready(function(){
var randomQuotes = [
"Doh",
"Mom, where's my meatloaf",
"This is spartan",
"Stay here,I will be back ",
"You talking to me? You talking to me??"
];
$("#quoteButton").on('click', function(){
function getRandomQuotes() {
return Math.floor(Math.random() * randomQuotes.length);
}
$("#quotesBox").text(randomQuotes[getRandomQuotes()]);
});
});

Why code that works on jsfiddle doesn't work locally?

I take a script from jsfiddle, and obviously it work, but when i try to run it locally, it stops to work. I read in another question that to use jsfiddle code, I need to put my script into:
$(document).ready(function() {
});
I do it for the following code, but it doesn't work even. Can someone please help me?
<html>
<head>
<style type="text/css">
li {
border: 1px solid #ccc;
margin-bottom:3px;
padding: 2px 5px;
}
button {
margin-left: 10px
}
</style>
<script type=”text/javascript”>
$(document).ready(function() {
$('#btnName').click(function(){
var text = $('#inputName').val() + '<button>x</button>';
if(text.length){
$('<li />', {html: text}).appendTo('ul.justList')
}
});
$('ul').on('click','button' , function(el){
$(this).parent().remove()
});
});
</script>
</head>
<body>
<input type="test" id="inputName" />
<button id="btnName">Add</button>
<ul class="justList"></ul>
</body>
</html>
Two reasons:
Because you haven't included jQuery on the page. You need
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
somewhere before your script (with that path or some other valid path to jQuery).
The quotes here
<script type=”text/javascript”>
are fancy quotes. They must be normal quotes. Or, best practice, just don't include the type at all; JavaScript is the default.
It's also worth formatting your code in a reasonable, consistent way.
E.g.:
<html>
<head>
<style type="text/css">
li {
border: 1px solid #ccc;
margin-bottom:3px;
padding: 2px 5px;
}
button {
margin-left: 10px
}
</style>
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(document).ready(function() {
$('#btnName').click(function() {
var text = $('#inputName').val() + '<button>x</button>';
if (text.length) {
$('<li />', {
html: text
}).appendTo('ul.justList')
}
});
$('ul').on('click', 'button', function(el) {
$(this).parent().remove()
});
});
</script>
</head>
<body>
<input type="test" id="inputName" />
<button id="btnName">Add</button>
<ul class="justList"></ul>
</body>
</html>

Add on ready to my code

I'm trying to make a list that allows a user to submit issues. Im using knockout and i can get it to do exactly what i wanted it to do but when i try to debug in microsoft visual studios it does not work they way I want it to. When I debug, the page opens the same as in the fiddle except the "test issue" is missing from the issue list. Also you can type in the add issue text box but when you hit submit it clears and does not add to the issue list
I was told I needed to add an onready, but im still new to learning how to code and am unsure of
A.How to do it
B. Where to put it
Here is my fiddle
http://jsfiddle.net/grahamwalsh/rCB9V/
and here is my code
IssueList ( html )
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Issue List</title>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script src="Issuelist.js"></script>
<link type="text/css" rel="stylesheet" href="Issuelistcss.css" />
</head>
<body>
<div class='issuelist'>
<form data-bind="submit:addIssue">
Add Issue: <input type="text" data-bind='value:issueToAdd, valueUpdate: "afterkeydown"' />
<button type="submit" data-bind="enable: issueToAdd().length > 0">Add</button>
</form>
<p>Your Issues:</p>
<select multiple="multiple" height="5" data-bind="options:allIssues, selectedOptions:selectedIssues"> </select>
<div>
<button data-bind="click: removeSelected, enable: selectedIssues().length > 0">Remove</button>
<button data-bind="click: sortIssues, enable: allIssues().length > 1">Sort</button>
</div>
</div>
</body>
</html>
Css
body { font-family: arial; font-size: 14px; }
.issuelist { padding: 1em; background-color: #87CEEB; border: 1px solid #CCC; max-width: 655px; }
.issuelist input { font-family: Arial; }
.issuelist b { font-weight: bold; }
.issuelist p { margin-top: 0.9em; margin-bottom: 0.9em; }
.issuelist select[multiple] { width: 100%; height: 8em; }
.issuelist h2 { margin-top: 0.4em; }
js
var Issuelist = function () {
this.issueToAdd = ko.observable("");
this.allIssues = ko.observableArray(["test"]);
this.selectedIssues = ko.observableArray(["test"]);
this.addIssue = function () {
if ((this.issueToAdd() != "") && (this.allIssues.indexOf(this.issueToAdd()) < 0))
this.allIssues.push(this.issueToAdd());
this.issueToAdd("");
};
this.removeSelected = function () {
this.allIssues.removeAll(this.selectedIssues());
this.selectedIssues([]);
};
this.sortIssues = function () {
this.allIssues.sort();
};
};
ko.applyBindings(new Issuelist());
To run a function when the page is ready using jQuery:
$(document).ready(function(){
//Code goes here
}

Closed Div by default

HTML Code
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.msg_body
{
display: none;
}
</style>
<script language="javascript" src="Scripts/jquery-1.4.1.min.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(".msg_head").click(function () {
$(this).next(".msg_body").slideToggle();
})
$(".msg_headRoot").click(function () {
$(this).next(".msg_bodyRoot").slideToggle();
})
.toggle(function () {
$(this).children("span").text("[+]");
}, function () {
$(this).children("span").text("[-]");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="msg_headRoot" style="cursor: pointer;">
<p class="msg_headRoot" style="margin: 3px; line-height: 20px; font-size: 16px; font-weight: bold;">
<span>[+] </span>Root
</p>
<div class="msg_bodyRoot">
<div class="msg_head" style="cursor: pointer;">
<p class="msg_head" style="margin: 3px; line-height: 20px; font-size: 16px; font-weight: bold;">
<span>[+] </span>Hello
</p>
<div class="msg_body">
Div text
</div>
</div>
</div>
</div>
</form>
</body>
</html>
Query
Right now it is keeping the div opened. I want to keep the div closed by default and user should click the plus to open it.
EDIT
Forgot to mention the root div. I have actually one nested div. I meant on click the plus of outer dive should open the inner div
CSS:
.msg_body, .msg_bodyRoot {display: none;}​
Updated JS:
$(document).ready(function() {
var toggler = function(elem) {
elem.slideToggle(500, function() {
if (elem.is(':visible')) {
elem.parent().find("p:first > span").text("[-]");
} else {
elem.parent().find("p:first > span").text("[+]");
}
});
};
$('div.msg_headRoot').click(function(e) {
toggler($(this).children(".msg_bodyRoot"));
e.preventDefault();
return false;
});
$("div.msg_head").click(function(e) {
toggler($(this).children(".msg_body"));
e.preventDefault();
return false;
});
});
Working fiddle: http://jsfiddle.net/N9E9b/2/
Add some CSS to your markup:
.msg_body { display: none; }
You are missing a semi-colon, and no toggle on the inner part. I made some assumptions on my part that the root should work with its child and the inner with its child. I surely hope I have not made an oversimplification of what you want here.
EDIT: REMOVED BAD CODE THAT HAD BUGS
Just to follow up with the simpler: (and fix a minor issue with +/- text in prior version)
$(document).ready(function() {
$(".msg_head,.msg_headRoot").click(function() {
$(this).next(".msg_body").slideToggle();
var clickp = $(this).children("span:contains('+')");
var clickm = $(this).children("span:contains('-')");
clickp.text("[-]");
clickm.text("[+]");
});
});
<div>
<p class="msg_headRoot"><span>[-] </span>Root</p>
<div class="msg_body">
<p class="msg_head"><span>[+] </span>Hello</p>
<div class="msg_body">Div text</div>
</div>
</div>
.msg_headRoot,.msg_head{margin: 3px; line-height: 20px; font-size: 16px; font-weight: bold; cursor:pointer;}
.msg_body div.msg_body{display:none;}
Example here: http://jsfiddle.net/ykRaY/3/

Categories

Resources