See it live here: http://evanwknight.com/4240/#about. The variables go into local storage, but not until you click send message, then refresh the page.
<form id="form-one" name="form-one">
<div class="form-group-one">
<textarea class="names" id="message" rows="1">Hey Evan! ; )</textarea>
</div><br>
<button class="btn btn-success" id="submit" type="submit">Send Message</button>
</form>
// where the variables show
<div class="messages" id="onee">
<span class="bubble you" id="displayMessage"></span>
<div id="myMessage">
<span class="MYbubble me">Hey there! <img src="img/battery.png" width="15px"></span>
</div>
// js
<script type="text/javascript">
$(document).ready(function () {
$('.messages').hide();
$('#submit').click(function (e) {
e.preventDefault();
$('#onee').show();
$('#myMessage').fadeIn(2000);
$('#form-one').hide();
$('#form-two').show();
var displayMessage = $('#message').val();
localStorage.displayMessage = displayMessage;
});
$('#displayMessage').html(localStorage.displayMessage);
});
</script>
It's not showing up till after the reload because you're only setting it after the form is submitted.
You would need to put
$('#displayMessage').html(localStorage.displayMessage);
into the click event as well as after you set it.
...
localStorage.displayMessage = displayMessage;
$('#displayMessage').html(localStorage.displayMessage);
});
$('#displayMessage').html(localStorage.displayMessage);
Related
Hey the task i try to achieve is to activate and deactivate a button depending on, if there is text in a textarea or not.
Here the code I'm trying to get going.
var button = document.getElementById("button_01");
var textarea = document.getElementById("textarea_01");
$(button).prop('disabled', true);
textarea.addEventListener('keyup', function() {
button.disabled = !this.value;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="div_01">
<h1>String Editor</h1>
<textarea id="textarea_01" rows="5" placeholder="Paste your string here"></textarea>
</div>
<div id="div_02">
<h1>Options</h1>
<button id="button_01" type="button" value="submit">
Modify
</button>
</div>
and here a fiddle: Fiddle
What I'm really not understanding here is, why the button which I disabled in the js part is still active
I'm an absolute html/js/web developement newbie, so please give some explanation.
Thanks in advance
The jsFiddle doesn't work because there's no jQuery CDN link.
You can find an updated version, witch includes the jQuery CDN here.
You're using jQuery to disable the button on page-load.
I'll recommend using HTML for that:
<button id="button_01" type="button" value="submit" disabled>
Then, your code appears to work just fine because there's no jQuery needed anymore:
var button = document.getElementById("button_01");
var textarea = document.getElementById("textarea_01");
textarea.addEventListener('input', function() {
button.disabled = !this.value;
});
<div id="div_01">
<h1>String Editor</h1>
<textarea id="textarea_01" rows="5" placeholder="Paste your string here"></textarea>
</div>
<div id="div_02">
<h1>Options</h1>
<button id="button_01" type="button" value="submit" disabled>
Modify
</button>
</div>
Note;
Changed event from keyup to input so things like right-click and paste will trigger the event. (Thx to #freedomn-m)
Try like this.
var button = document.getElementById("button_01");
var textarea = document.getElementById("textarea_01");
button.disabled = true;
textarea.addEventListener('keyup', function() {
button.disabled = !this.value;
});
<div id="div_01">
<h1>String Editor</h1>
<textarea id="textarea_01" rows="5" placeholder="Paste your string here"></textarea>
</div>
<div id="div_02">
<h1>Options</h1>
<button id="button_01" type="button" value="submit">
Modify
</button>
</div>
Hi all i am just trying to remove my form when i click on a button but i have no results. I am sure that function "sign()" was called but with no effect on my layout:
<body>
<div id="divSign">
<form action="#" method="post">
<div class="header">
<p>All Together</p>
</div>
<div class="description">
<p>Hi friend</p>
</div>
<div class="input">
<input type="text" class="button" id="chiave" placeholder="Chiave Segreta">
<input type="submit" class="button" id="submit" value="ENTRA" onclick="sign();">
</div>
</form>
</div>
</body>
<script>
function sign() {
document.body.removeChild(document.getElementById("divSign"));
// OR THIS
var f = document.getElementById("divSign");
while (f.hasChildNodes()) {
f.removeChild(f.lastChild);
}
// OR THIS
var f = document.getElementById("divSign").style.visibility = "hidden";
}
</script>
Nothing seems affect my page, i just to clear my page after click.
On clicking, your form get submitted and the page refreshs.
Use this onclick instead, the return false; will prevent the form from being submitted and your page will not be refreshed anymore :
<input type="submit" class="button" id="submit" value="ENTRA" onclick="sign(); return false;">
It's not removed because the page is reloaded after your click on the button, because it's a form. To prevent that, you can use return false; at the end of your function.
function sign() {
/*********
Your code
**********/
return false; //add this line
}
With jquery you can do by using empty
$("#divSign").empty()
or
$("#divSign form").remove()
How can i change behaviour of button if form submit was successful?
<form method="post" enctype="multipart/form-data" style="margin: 0;" id="frmGenerate">
<div class="clearfix">
<div class="input-group">
<input type="text" placeholder="Customer Name:" name="CustomerName">
<input type="text" placeholder="Generated Url:" name="CustomerUrl" readonly="readonly" />
</div>
</div>
<div class="clearfix">
<div class="input-group">
<button type="submit" class="btn">Generate</button>
</div>
</div>
</form>
And here is my JS code:
$('#frmGenerate').submit(function(e) {
var clientName = $(this).find('input[name="CustomerName"]').val();
$.ajax(
{
url: '#Url.Action("GenerateToken","Admin")',
type: 'GET',
data: { customerName: clientName },
success: function(response) {
if (response.result) {
$('#frmGenerate').find('input[name="CustomerUrl"]').val(response.message).select();
$('#frmGenerate').find('button.btn').text('Close');
} else {
alert(response.message);
}
}
});
e.preventDefault();
});
This is modal windows, which i open to generate token. When it's generated successful then i set generated token in modal input and change text of button to close, but i don't know how to change button's behaviour.
Better approach would be not to change existing button, but hide it and show another.
HTML:
<div class="clearfix">
<div class="input-group">
<button type="submit" class="btn">Generate</button>
<button type="button" class="btn btn-close hide">Close</button>
</div>
</div>
JS:
$('#frmGenerate').find('.btn').hide();
$('#frmGenerate').find('.btn-close').show();
Depends on what type of button you are using. Button must be used like: <button></button> Not like <button/>
And then use:
$(".btn").html('Close');
you can change attribute of button
$('#frmGenerate').find('button.btn').attr('type', 'button');
and then:
$('#frmGenerate').find('button.btn').on('click', function(){ /*close function */});
I see you have changed the text of your button to 'Close' after the form has been submitted. So the remaining part is the jQuery that handles a click event on the button.
....
$('.btn').click(function(){
var buttonText = $(this).val();
if(buttonText === 'Close'){
//code when close button is clicked
}else if(buttonText === 'Generate'){
//code when Generate button is clicked...
}
});//end button.click function
And I would use input type='button' or button than input type='submit'. Why dont you submit form using ajax as well? much better.
Hope this helps...
I am very new to jQuery and I'm looking for an explanation as to why this code does not seem to work. I think it is something with the "action" not sure. Can someone help me understand my mistake here. thanks
<script src="/jquery.validationEngine.js"></script>
<script>
$("#contact_body").submit(function(e) {
e.preventDefault(); // Prevents the page from refreshing
var $this = $(this); // `this` refers to the current form element
if ($("#contact_body").validationEngine('validate')) {
//Post Data to Node Server
$.post(
$this.attr("action"), // Gets the URL to sent the post to
$this.serialize(), // Serializes form data in standard format
function(data) { /** code to handle response **/ },
"json" // The format the response should be in
);
//Notify User That the Email Was Sent to the Server & Thanks!
//$('#contactThanksModal').modal('show');
$('#contactModal').modal('hide');
alert("success");
}
else {
//handle Invalid Email Format Error
alert("error");
}
});
</script>
<!--pop up contact form -->
<div id="contact" class="modal hide fade in" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Send us a message</h3>
</div>
<div class="modal-body">
<form id="contact_body"class="contact_body" name="contact_body" action="/contact">
<label class="label" for="form_name">Your Name</label><br>
<input type="text" name="form_name" id="form_name" class="input-xlarge"><br>
<label class="label" for="form_email">Your E-mail</label><br>
<input type="form_email" name="form_email" class="input-xlarge"><br>
<label class="label" for="form_msg">Enter a Message</label><br>
<textarea name="form_msg" class="input-xlarge"></textarea>
</form>
</div>
<div class="modal-footer">
<input class="btn btn-success" type="submit" value="Send!" id="submit">
Nah.
</div>
<!-- <div id="thanks"><p><a data-toggle="modal" href="#contact" class="btn btn-primary btn-large">Modal powers, activate!</a></p></div> -->
You need to wrap your JQuery scripts in a
$(document).ready(function() {
...your_code_here...
});
This will then wait for the whole document to load before trying to attach events to objects.
Without this you may be trying to bind events to objects that have yet to be "created".
You need to put your code in a document ready handler:
<script src="/jquery.validationEngine.js"></script>
<script>
$(function() {
$("#contact_body").submit(function(e) {
// your code...
});
});
</script>
Your code is currently trying to add the submit() handler before the element exists in the DOM.
I want to Hide a div On a button click. Here I am having 2 divs (Login and container). When the page loads, container will be hidden. And On click on a #submit button, I want to show the container div and hide the Login div.
The problem is Now login page is showing when the page loads, but After clicking the button I cannot make the div hide or show
Javascript:
<script>
$('#submit').click(function() {
console.log("Called2")
$('#container').show();
$('#login').hide();
});
</script>
HTML:
<div id="login" >
<div id="loginformcontainer">
<form>
<label> Username:
<input type="text" name="uname">
</label>
<label> Password:
<input type="password" name="pwd">
</label>
<div class="buttons1">
<button type="button" id="submit" value="Login" class="btn">
Submit
</button>
<button type="reset" id="reset" value="Reset" class="btn">
Clear All
</button>
</div>
<div>
<div id="container" style="display: none;">
<p> Index Page</p>
</div>
Write your code in document.ready function so it work,
$(document).ready(function(){
$('#submit').click(function() {
console.log("Called2")
$('#container').show();
$('#login').hide();
});
});
or, its shorthand,
$(function(){
$('#submit').click(function() {
console.log("Called2")
$('#container').show();
$('#login').hide();
});
});
for changing its visibility every time you can use toggle() function like,
$(function(){
$('#submit').click(function() {
console.log("Called2")
$('#container, #login').toggle();
});
});
See if this is the issue, you have not a closing </div> tag here:
</div>
<div> //<----------------here you can see an opening tag instead
<div id="container" style="display: none;">
put a closing </div> above.
and try putting your script in doc ready:
$(function(){
$('#submit').click(function() {
console.log("Called2")
$('#container').show();
$('#login').hide();
});
});
As it is a submit button handling the forms, submit event might be more useful. In this way you can validate and prevent from submitting.
Please check the jQuery docs for submit.
Look on the demo
$('#container').hide();
$('#submit').click(function() {
console.log("Called2")
$('#container').show();
$('#login').hide();
})
I believe that you not declare the <div id="container" ></div>
Write your script in document.ready inside the <script> function so it work...
Or you can't declare the library of Jquery.
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Your html
<div id="signIn" >Sign In</div>
<div id="login" >
<div id="loginformcontainer">
<form>
<label> Username:
<input type="text" name="uname">
</label>
<label> Password:
<input type="password" name="pwd">
</label>
<div class="buttons1">
<button type="button" id="submit" value="Login" class="btn">
Submit
</button>
<button type="reset" id="reset" value="Reset" class="btn">
Clear All
</button>
</div>
<div>
Your Jquery
$('#login').hide();
$('#signIn').click(function() {
alert("try");
});
$("#submit").click(function() {
$('#login').fadeOut();
//do your code
});
Upon page loads, the login div will hide and if you click the sign in then login div will just fade in. If the user clicks the submit button, before you hide the login div, make sure you will validate the user action. Refer to jquery documentation for further explanation.
Isn't submit button causing postback and thus you're loosing css style set by javascript.