clearing an input with javascript [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to clear the email input box when function(data) is greater than zero. The user will have to be force to re-enter another email address since the form will not submit if any fields are blank. how would I clear out the input box?
javascript:
$("#email").change(function(){
var email=$("#email").val();
$.ajax({
type:"post",
url:"/files/reg_check.php",
data:"email="+email,
success:function(data){
if(data == 0){
$("#email_msg").html("E-mail Address must be valid");
} else {
$("#email_msg").html("<div class='msg_check_red'>Error: E-mail address already in use.</div>");
}
}
});
});
html:
<label>E-mail:</label>
<input type="email" placeholder="your#email.com" id="email" name="add[email]" size="20" title="A valid email address required" class="{validate:{required:true,email:true}}" />
<span class="rsmall"><div id="email_msg">E-mail Address must be valid</div></span><br />

Use $("#email").val("");, which sets the value of the input to an empty string.

Related

Keeping entered value on html <textarea> with pages having multiple <form> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My php page consist of a number of textfileds and textareas in which the user can input their details there. these textareas and textfileds are inside a <form></form>.so when clicking on submit button all those values are posted (form method=POST ).
there are 2 <textarea> named as Permanent Address and Communication address. and also a check box "Communi. address is same as permanent". the check box code is follows
<input type="checkbox" id="checkadd" onclick="if (this.checked) copyAddress (this.form)"/>
this function calls copyaddress function that copy the Permanent address to Communication address <textarea>. " copyAddress (this.form) " will copy the value of 1st <textarea>(permanent address). so i don't get the value of Permanent address in the page in which i have to POST this value too to the Next Page.
Can you try this..
<?php
if($_POST['permaddress'])
{
echo $_POST['permaddress'];
echo $_POST['commaddress'];
}
?>
<script>
function copyAddress()
{
if(document.getElementById('checkadd').checked == true)
{
document.getElementById('commaddress').value = document.getElementById('permaddress').value;
}
}
</script>
<form method="post" action="test.php" >
<textarea rows="30"cols="30" name="permaddress" id="permaddress"></textarea>
<textarea rows="30"cols="30" name="commaddress" id="commaddress"></textarea>
<input type="checkbox" id="checkadd" onclick="copyAddress()"/>
<input type="submit" value="submit">
</form>

Javascript Incomplete tags [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I work for a nascent online retailer in Ghana, West Africa. We purchased an E-commerce template from Big Commerce. On our website there's a text-field where users are allowed to enter their email addresses and receive newsletters subsequently. Unfortunately, when users enter information, its not seen at the back end. They don't receive the automatic email once they hit enter. i suspect there's something wrong with Javascript code. Please advise:
<script type="text/javascript">
//
$('#subscribe_form').submit(function() {
if($('#nl_email').val() == '') {
// alert('You forgot to type in your email address.');
$('#nl_email').focus();
return true;
}
if($('#nl_email').val().indexOf('#') == -1 || $('#nl_email').val().indexOf('.') == -1) {
//alert('Please enter a valid email address, such as john#example.com.');
$('#nl_email').focus();
$('#nl_email').select();
return true;
}
// Set the action of the form to stop spammers
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
return true;
});
</script>
Syntax error here
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
Fix:
$('#subscribe_form').append("<input type='hidde' name='check' value='1' />");
Try putting:
$('#subscribe_form').append("<input type="hidden" name="check" value="1" />");
as
$('#subscribe_form').append('<input type="hidden" name="check" value="1" />');
or it will raise an error because of the quotes...

Using ajax/jQuery to update a list on button click [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm just wondering if anyone can point me in the right direction on how to create a dynamic list on a webpage based on user entry without refreshing the page each time.
For instance, a user would:
type a word into a textbox
click a button
the word would show up somewhere else on the page
the word would be erased from the textbox
Any new word typed would automatically be written underneath any previously entered words.
<div id="target"><div>
<input type="text" id="newInput" />
<input type="button" id="saveInput" />
<script>
// when button is pressed:
$('#saveInput').on('click', function(){
// check if something is there
if( $('#newInput').val().length !==0){
$('#target').append('<div>'+ $('#newInput').val()+'</div>'); //append to a target
$('#newInput').val(''); // empty input
$('#newInput').focus() // for bonuspoint, place cursor back in input
}
});
</script>
If you want each new entry as first listitem, use prepend() instead of append()
Here's an working example:
Html:
<input type="text" id="txtWord"/>
<input type="button" id="btnAddWord" value="Add new word" />
<ul id="words">
</ul>
Script:
$(document).ready(function(){
$('#btnAddWord').bind('click', function(){
if( $('#txtWord').val() !== ""){
$('#words').append('<li>'+ $('#txtWord').val()+'</li>');
$('#txtWord').val('');
}
});
});
Example
http://jsfiddle.net/AfNgH/

Cannot get form to validate [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is nothing validates. I can leave the form empty and it gives no alert
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
function validateForm() {
var x = document.forms["myform"]["fname"].value;
if (x == null || x == "") {
alert("Enter your certificate address first.");
} else if(!x.match(/facebook/g) || !x.match(/access_token/g)) {
alert("Invalid certificate address, please try again...");
return false;
}
)
</script>
<form name="myform" action="welcome.php" method="post" onsubmit="return validateForm();">
Name: <input type="text" name="fname">
<input type="submit" value="submit">
</form>
</body>
</html>
not sure what is wrong with this. looks pretty straitforward
Missing your last end bracket. Changes ) to }

jQuery .val using ID selector undefined [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
<form action="javascript:message();">
<input type="text" id="#msg" value='test'>
</form>
This is a simple form I made. Now when I call my JS function using this code it works (only one input can be accessed though):
function message() {
alert($('input').val());
}
But using this code doesn't work (alerts Undefined):
function message() {
alert($('#msg').val());
}
I have really no clue what to do and I have been looking for hours...
The value of the id attribute shouldn't start with a #:
<form action="javascript:message();">
<input type="text" id="msg" value='test'>
</form>
Here's the fiddle: http://jsfiddle.net/XrJjU/
If, for whatever reason, there is a # in the id, you'll have to escape it in your selector:
$('#\\#msg').val();
Here's the fiddle: http://jsfiddle.net/7P5ER/

Categories