How would I create a text box in jquery that would appear when a certain button is clicked? Currently I have code that allows me to click a button and it shows an image and comments from a database. What I want it to do is also so a comment box so you can add more comments to each image.
<!DOCTYPE html>
<html>
<head>
<cfquery datasource="AccessTest" name="qTest">
SELECT P.Account, P.Image, P.Image_ID, C.Remarks, C.Users, C.Accounts, C.Date_Time
FROM PictureDB AS P
INNER JOIN CommentsDB AS C
ON C.Image_ID = P.Image_ID
ORDER BY P.Image_ID
</cfquery>
<script src="http://code.jquery.com/jquery-2.0.3.js">
</script>
<script>
$(document).ready(function(){
var images = {
<cfoutput query="qTest" group="Image_ID">
"#qTest.Image_ID#": {
"image": "#qTest.Image#",
"remarks": [
<cfoutput>
"#qTest.Users#, #qTest.Date_Time# <br> #qTest.Remarks# <br> </br>",
</cfoutput>
]
},
</cfoutput>
};
$("button").click(function(event){
event.preventDefault();
var id = $(this).data("id");
var src = images[id].image;
var desc = images[id].remarks.toString();
$("#theImage").attr("src", src).removeClass("hide");
$("#theDescription").html(desc).removeClass("hide");
});
});
</script>
</head>
<body>
<cflayout name="myAccordionLayout" type="accordion" width="600px">
<cflayoutarea title="Bill Analysis" align="left">
<cfoutput query="qTest" group="Account">
<button data-id="#qTest.Image_ID#">
#qTest.Account#
</button>
</cfoutput>
</cflayoutarea>
</cflayout>
<img id="theImage" class="hide">
<div id="theDescription" class="hide">
</div>
</body>
</html>
Then currently I am trying to work this code into the above code.
<html>
<script src="http://code.jquery.com/jquery-2.0.3.js">
</script>
<script>
$(document).ready(function(){
$("#addcomment").click(function ()
<!--- $("#postComment").show("slow");--->
});
});
</script>
<cfform name="InsertComments" id="InsertComments">
<fieldset>
<div id="container">
<div id="mainContent">
<div id="addcomment"> <a href='#'>add comment</a></div>
<div id='postComment'>
<cftextarea name="Remarks" cols="55" rows="4" label="Tour Description"
required="yes" validateat="OnSubmit" message="Please enter your comment here"
enabled="no">
</cftextarea>
<cfinput type="text" name="Image_ID" message="Please enter Account Here."
validateat="onSubmit" required="yes" id="Image_ID" size="10"
maxlength="60">
</cfinput>
<cfinput type="submit" name="insertComments" value="Insert Comments" id="submit">
</cfinput>
</div>
</div>
</fieldset>
</cfform>
<cfif IsDefined("form.InsertComments")>
<cfquery datasource="AccessTest">
INSERT INTO CommentsDB (Remarks, Image_ID, Date_Time )
VALUES
(<cfqueryparam value="#form.Remarks#" cfsqltype="CF_SQL_LONGVARCHAR">
</cfqueryparam>
, <cfqueryparam value="#form.Image_ID#" cfsqltype="cf_sql_integer">
</cfqueryparam>
, <cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp">
</cfqueryparam>
)
</cfquery>
</cfif>
</div>
</html>
For the pop-up text box, you could try adding a jQueryUI .dialog() widget to your code, as per this example.
I'm not a cf guy, but something like this should get you started:
jsFiddle Demo
Notes:
.1. Sorry, I couldn't get the <img> tag to behave properly (hide correctly) and don't have time to troubleshoot it. Should be easy to figure out.
.2. Had to change your <button> element to an <input type="button" /> because the button tag was wreaking havoc with jQueryUI's internal buttons: functionality.
.3. Renamed your button ID so it would work with the non-cf, jsFiddle example.
.4. Since you are now also using jQueryUI's library, it must be referenced in your code along with jQuery itself. Note: you also must reference one of the stylesheet themes for jQueryUI. All together, it will look like this:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
</head>
HTML:
<input type="button" id="bbb" data-id="#qTest.Image_ID#" value="#qTest.Account#" /><br />
<br />
<img id="theImage" class="hide" src="http://placehold.it/150x150" ><br>
<div id="theDescription" class="hide" ></div>
<div id="msgbox">
<p>Please enter any additional comments:</p>
<textarea id="ta" rows="5" cols="30"></textarea>
</div>
jQuery/js:
$('#msgbox').dialog({
autoOpen:false,
modal:true,
title: 'Add Comments',
buttons: {
Okay: function() {
var oldComments = $("#theDescription").html();
var newComments = $('#ta').val();
$("#theDescription").html(oldComments +'<br />' + newComments);
//Do your ajax update here:
/*
$.ajax({
//Unsure of cfc syntax
});
*/
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
},
close: function() {
alert('AJAX update completed');
}
});
$("#bbb").click(function (event) {
event.preventDefault();
var id = $(this).data("id");
var src = 'http://placehold.it/150x150';
var desc = 'This is the first bit of remarks';
$("#theImage").attr("src", src).removeClass("hide");
$("#theDescription").html(desc).removeClass("hide");
$('#msgbox').dialog('open');
});
Related
I have looked at multiple solutions on stack overflow and non of them have worked.
When I click the button, it take me to a new url yoursite.com/index.html?message=blah&pass=12
I want it to stay in the same page and just execute the script I wrote.
Please help
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Encryption Using Caesar</title>
<link rel="stylesheet" href="https://unpkg.com/marx-css/css/marx.min.css">
<style>
.hidden{
visibility: hidden;
}
</style>
<script src="encrypt.js"></script>
</head>
<body>
<main>
<h2>What is this?</h2>
<p>This application was built to <b>encrypt</b> data using the <b>Caesar method.</b>
All encryption is handled on <i>client side</i> in order to garuntee <b>data integrity.</b> </p>
<form onsubmit=" event.preventDefault; doStuff(); ">
<h5>Insert some text in the following box:</h5> <br>
<textarea id="userText" name="message" cols="30" rows="10" placeholder="Your secret message" autofocus></textarea>
<br>
<h5>Enter Your Favorite Number</h5> <br>
<input type="text" name="pass" id="pass" placeholder="Enter Your Favorite Number:"> <br>
<button id="encrypt" type="submit" onclick="return false;" style="width: 100%;">Lets Encrpt</button>
<div id="emessage"></div>
</form>
</main>
<script>
window.onload = function(){ doStuff(); };
function doStuff(){
document.getElementById("encrypt").onclick = function(){
var key = document.getElementById("pass").value;
var message = document.getElementById("userText").innerHTML;
var eMessage = caesarShift(message,key);
document.getElementById("emessage").innerHTML = eMessage ;
}
}
</script>
</body>
</html>
I have another js file called encrypt.js which has a function called caesarShift()
I don't know why this isnt working. Please help!
Change event.preventDefault to event.preventDefault().
The former only references the function while the latter actually invokes it.
I changed the code a bit to make it more simple to do the stuff you are looking for.
You need to define the caesarShift function that you are calling. Unless you define that you won't get the answer you are looking for in the div emessage.
Also you need to change to event.PreventDefault() if you want to use your method only and then define the caesarShift and then call it.
HTML Part -->
<form>
<h5>Insert some text in the following box:</h5> <br>
<textarea id="userText" name="message" cols="30" rows="10" placeholder="Your secret message" autofocus></textarea>
<br>
<h5>Enter Your Favorite Number</h5> <br>
<input type="text" name="pass" id="pass" placeholder="Enter Your Favorite Number:"> <br>
<button id="encrypt" type="button" onclick="doStuff()" style="width: 100%;">Lets Encrpt</button>
<div id="emessage"></div>
</form>
JS part -->
<script>
function doStuff(){
var key = document.getElementById("pass").value;
var message = document.getElementById("userText").value;
// Define this caesarShift function
var eMessage = caesarShift(message,key);
document.getElementById("emessage").innerHTML = eMessage ;
}
// window.onload = function(){ doStuff(); };
</script>
UPDATE:
Somehow the just work fine after I tried it..
UPDATE
I'm using .click() function on my javascript, but it doesn't work.
Here is my JS code:
src="jquery.js";
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var institutionID, acadCareerID;
$(document).ready(function(){
getInstitution();
institutionID = $( "select#institution option:checked" ).val();
if(institutionID == null){
institutionID = 1;
}
getAcadCareerByInstitution(institutionID);
acadCareerID = $( "select#acadCareer option:checked" ).val();
if(acadCareerID == null){
acadCareerID = 1;
}
getPeriodByAcadCareerAndInstitution(acadCareerID);
getDepartmentByAcadCareer(acadCareerID);
$("select#institution").change(function(){
institutionID = $( "select#institution option:checked" ).val();
getAcadCareerByInstitution(institutionID);
})
$("select#acadCareer").change(function(){
acadCareerID = $( "select#acadCareer option:checked" ).val();
getPeriodByAcadCareerAndInstitution(acadCareerID);
getDepartmentByAcadCareer(acadCareerID);
})
$("div#search").click(function(){
alert("The paragraph was clicked.");
console.log("abc");
});
});
all functions like getInstituion(), getAcadCareerByInstitution(institutionID) etc are ajax call.
Here is my HTML code:
<form action="doInsertSchedule" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="institution">Institution</label>
<select name="institution" class="form-control" id="institution">
</select>
</div>
<div class="form-group">
<label for="acadCareer">Academic Career</label>
<select name="acadCareer" class="form-control" id="acadCareer">
</select>
</div>
<div class="form-group">
<label for="period">Period</label>
<select name="period" class="form-control" id="period">
</select>
</div>
<div class="form-group">
<label for="department">Department</label>
<select name="department" class="form-control" id="department">
</select>
</div>
<div class="form-group">
<label for="date">Deadline Date</label>
<input type="date" class="form-control" id="deadline" placeholder="Deadline Date" name="deadline">
</div>
<button type="submit" class="btn btn-default">Submit</button>
<div id="search" class="btn btn-default">Search</div>
</form>
I already put jquery for my Master Layout (Laravel 5)
<head>
<link rel="stylesheet" type="text/css" href="{{url()}}/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{{url()}}/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="{{url()}}/js/tools.js"></script>
<script src="{{url()}}/js/bootstrap.min.js"></script>
<title>BINUS - #yield('title')</title>
<meta name="csrf-token" content="{{ csrf_token() }}" />
</head>
I want the Search Button / Div to do something when I click. I can't see what's wrong here.
I already tried <div> to <button> but it just will work like submit. I also already tried to put event.preventDefault() at my JS code, but it didn't work too.
Maybe you can try with event delegation...
$(document).ready(function(){
$("body").on("click", "#search", function(){
alert("The paragraph was clicked.");
console.log("abc");
});
});
You code works OK. You need to write the jquery library in the final, just before of </body>
...
<script type="text/javascript" src=".../jquery.js"></script>
</body>
Example it works ok!: Example Jsfiddle
<button type="submit" class="btn btn-default">Submit</button>
<div id="search" class="btn btn-default">Search</div>
This looks like it will render on the page as two buttons. Your ID here is attached to the div, not to the button, they should both appear to look like buttons providing bootstrap is loaded, but only the div with "search" as the content will do anything.
This is the only thing that appears odd, the only other problem is you are not loading your jquery as stated elsewhere.
I don't have the reputation to comment, but have you added a jquery library before your script?
Secondly, if you did and the items on the page are loaded after the document has loaded you will need to change your code up, for instance:
Give your form an id
<form action="doInsertSchedule" method="POST" enctype="multipart/form-data" id="postForm">
Then in your jquery script
<script>
$('#postForm').on('click', '#search', function(){
alert("The paragraph was clicked.");
console.log("abc");
});
</script>
The above will allow the javascript to run on items that are bought in after document load, however it is important you set it on a parent element that is avaiable at document load
If i had a button and an input field. How would i alert whatever is in the input field to the user, when the button is clicked.
Explain your code please.
Make it as simple as possible.
<input type="text" id="input" />
<button onclick="displayEnteredText()">Display</button>
<script>
function displayEnteredText() {
var inputText = document.getElementById("input"); // get the element with id "input" which is the textField
alert(inputText.value); // show the value of the input in alert message
}
</script>
One possible approach:
<!DOCTYPE html>
<html>
<head></head>
<body>
<input id="name" value="">
<input type="button" value="show me the name" onclick="alert(document.getElementById('name').value)">
</body>
</html>
Another possible approach:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function () {
var buttonElement = document.getElementById('button');
buttonElement.addEventListener('click', function() {
alert(document.getElementById('name').value);
});
}
</script>
</head>
<body>
<input id="name" value="">
<input id="button" type="button" value="show me the name">
</body>
</html>
With the second approach you can separate responsabilities, one person can create de html, and another person can focus in create javascript code.
Exists several ways to do this, but with two examples i think is enough in the current context
<body>
<input type="text" name="basicText" id="alertInput">
<button class="alertButton">Click me!</button>
</body>
<script type="text/javascript">
$(".alertButton").click(function(){
var value = $("#alertInput").val();
alert(value + " was entered");
});
</script>
In order to show what you typed in your alert, you need to reference the value inside the textbox. Since jquery is tagged in the post, I used it to get what's in the text box.
You can also try this one
HTML
<input type="button" id="btnclick" style="width:100px" value="Click Me" />
<input type="text" id="txtbox">
JS
$("#btnclick").click(function(){
var txtvalue = $("#txtbox").val();
alert("User enter " + txtvalue);
})
FIDDLE
Trying to change text color and background color of the text according to what I write in the textbox. Seems to work briefly; it shows me the color for a split second, like a quick snap and that's it.
<!DOCTYPE html>
<html>
<head>
<title>Prelab5 Ex1</title>
<style>
</style>
</head>
<body>
<h2>Prelab5 Ex1</h2>
<form method="post">
<input type="text" name="background" id="background"/><input type="submit" value="Background" onclick="changeBack();"/>
<br/>
<input type="text" name="text" id="text"/><input type="submit" onclick="changeText();" value="Text"/>
<br/>
<div id="content">Some text</div>
</form>
<script>
var DivText = document.getElementById("content");
function changeBack(){
var backColor = (document.getElementById("background").value);
DivText.style.backgroundColor= backColor;
}
function changeText(){
var textColor = (document.getElementById("text").value);
DivText.style.color = textColor;
}
</script>
</body>
</html>
Your onclick handlers for your submit buttons don't return false so your form is submitted resetting the page. You could add return false like
var DivText = document.getElementById("content");
function changeBack() {
var backColor = (document.getElementById("background").value);
DivText.style.backgroundColor = backColor;
}
function changeText() {
var textColor = (document.getElementById("text").value);
DivText.style.color = textColor;
}
<!DOCTYPE html>
<html>
<head>
<title>Prelab5 Ex1</title>
</head>
<body>
<h2>Prelab5 Ex1</h2>
<form method="post">
<input type="text" name="background" id="background" />
<input type="submit" value="Background" onclick="changeBack(); return false" />
<br/>
<input type="text" name="text" id="text" />
<input type="submit" onclick="changeText(); return false" value="Text" />
<br/>
<div id="content">Some text</div>
</form>
</body>
</html>
Your "submit" input performs the action on the form, which is currently set to "post". This will post the form to the same page (and cause a refresh).
You can override the default functionality by adding return false; to the ONCLICK attribute on all input type= "submit" elements.
In other words, this:
needs to become this:
<input type="submit" onclick="changeText();" value="Text"/>
but why use input type = submit at all?
You can just as easily use a link or button without the form:
<a href="#" onclick="changeText();"/>Test</a>
or
Click me!
which will do the same thing without needing to override the a forum action :)
Adding the insertion of input type text into the div title on the same screen. Attempted broken function to they turn the input into a variable and use it to fill in the div.
<body>
<div class="form">
Form <br><br>
<form name="input" action="javascript:alert(titleValue);">
Title: <input type="text" name="title" class="titleInsert">
<input type="submit" value="Submit">
</form>
</div>
<div class="offer_display">
<div class="title" id= title></div>
</div>
</body>
<script>
var titleValue="";
$('#titleInsert').on("change", function () {
titleValue=$(this).val();
$("#title h3").text("titleValue");
});
</script>
</html>
Another problem with you code is, that you have to use "input" event and not "change".
See working code on fiddle: http://jsfiddle.net/z037qv3n/
<body>
Form <br><br>
<form name="input" action="javascript:alert(titleValue);">
Title: <input type="text" name="title" id="titleInsert">
<input type="submit" value="Submit">
</form>
<div class="title" id="title"></div>
</body>
<script>
var titleValue="";
$('#titleInsert').on("input", function () {
titleValue=$(this).val();
$("#title").text(titleValue);
});
</script>
</html>
Change your code for $('div.title').html(titleValue);
Live Demo : JSFIDDLE
You have to include jQuery library in your head section like this
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
There are lot of mistakes in your code please see below:
First mistake you should put your jQuery inside ready function.
Second mistake there is no such id #titleInsert it is a class so you have to do .titleInsert
Third mistake there is no tag h3 in #title
Fourth mistake you should use .keyup event instead .change event
Replace your jQuery code with following :
<script type="text/javascript">
$(document).ready(function(){
var titleValue="";
$('.titleInsert').on("keyup", function () {
titleValue=$(this).val();
$("#title").text("titleValue");
});
});
</script>