How to trigger click event in jquery? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a js file with the following code to fire a click event. But the event is not working. The console given just above for jQuery.fn gives the result.
In my HTML I have a form with input values and a submit button with class submit.
Can anyone help?
(function(jQuery) {
"use strict";
console.log(jQuery.fn)
jQuery(".submit").on('click',function(e){
console.log('test')
e.preventDefault();
});
})(window.jQuery);
<form name="login-form">
<input id="email" type="email">
<input id="password" type="password">
<input type="submit" class="submit" value="submit">
</form>

.submit is a css-selector.Make sure you have a class submit on you submit button.
Here is working snippet:
(function(jQuery) {
"use strict";
//console.log(jQuery.fn)
jQuery(".submit").on('click',function(e){
console.log('test')
e.preventDefault();
});
})(window.jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" class="submit" value="Send Request">

Take a look at this http://api.jquery.com/trigger/
$('body').click(function() {
$('#submit').trigger('click');
});

Hope so this will help you.
(function (jQuery) {
jQuery('body').on('click', '.submit', function (e) {
console.log('test')
e.preventDefault();
});
})(window.jQuery);

Related

Javascript - Clear search output when submitting another text input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
For my project I need to create a simple search engine for my webpage.
I need a way to clear my previous search results when I submit another search into the form without refreshing the entire page.
My Javascript file contains the objects I am searching for.
HTML:
<form>
<div class="form-group">
<input id="term" class="form-control" type="text">
</div>
<button id="search-button" class="btn btn-default" type="button">
Search</button>
</form>
<div id="results-area"></div>
Correct me if i'm wrong:
Code with jQuery:
$(form).submit(function(e){
$("#results-area").html("");
})
Using jQuery it would go like this:
$("#search-button").click(function(){
// your search function here
$("#term).val('');
});

HTML5 form button onsubmit not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a simple form with two HTML5 buttons. Each button submits the form to a different php page. This is working well, but the 'onsubmit' function is never triggered. I want to show a dialog box before the delete.php page is called.
<form method="post">
<input type="text" name="fname">
<!-- HTML5 FORMACTION -->
<button type="submit" name="update" formaction="update.php">Update</button>
<button type="submit" name="delete" onsubmit="return confirm('Do you really want to delete?');" formaction="delete.php">Delete</button>
</form>
I have tried different variations, but the javascript code is never triggered. Why?
Buttons don't have submit events, forms do.
Buttons have click events.
onsubmit is valid for a form, not for a button. You will have to use onclick instead of onsubmit.
try like this
<form method="post">
<input type="text" name="fname">
<!-- HTML5 FORMACTION -->
<button type="submit" name="update" formaction="update.php">Update</button>
<button type="button" name="delete" onclick="return delete();" formaction="delete.php">Delete</button>
</form>
<script>
function delete(){
if(confirm('Do you really want to delete?')){
// delete code
}
return false;
}
</script>

Check if a text box starts with 92 using javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<form name="home" action="" onsubmit="return Validateform();" method="post">
<input type="text" name="receiver" />
<input type="submit" value="send" name="send" />
</form>
What is the javascript to alert if the textbox does not start with 92 on submit?
I recommend you read a JavaScript tutorial. Try here.
To answer your question, this will probably work. If you're wondering why you're getting downvoted - asking people to write your code for you is generally considered bad form, so try not to do this in future. :)
function Validateform(e) {
if (document.getElementsByName('receiver')[0].value.substring(0, 2) !== "92") {
e.preventDefault();
alert("Alert");
}
}

Jquery addClass does not work [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
I am starting with Jquery and i had this problem. I want click in the button send and have the input's border in red.
HTML CODE
<input type="text" placeholder="Put your name" id="name"/>
<input type="submit" value="send" id="send"/>
Javascript code
var name = document.getElementById('name');
$("#send").click(onClick);
function onClick() {
$("#name").addClass("error");
}
CSS STYLE
.error{
border-color:red;
}
If I put the .addClass out of the function click works and I don't know where is the problem?
$("#send").click(onClick); looks for an item with id=send; your button has value send, which is different. Add id="send" to your <input type="submit" value="send" onclick="send"/>
The full HTML code should look like this:
<input type="text" placeholder="Put your name" id="name"/>
<input type="submit" value="send" id="send"/>
Try using :
<input type="submit" value="send" id="send"/>
You were using onclick attribute to call a send fnction which do not exists.
And you where querying a #send id which do not exist either. I think you missed a modification when you wrote you JS code.
I actualy didn't get your need, but all i understand according to that, use this method
jQuery
$(function(){
$("#myform").on('submit',function(){onClick();return false;});
});
function onClick() {
$("#name").addClass("error");
}
HTML
<form id="myform">
<input type="text" placeholder="Put your name" id="name"/>
<input type="submit" value="send" id="send"/>
</form>
you need to use form tag to make submit button works and to control its working, and also you need to use function on "submit" event so the code can sense that it should have to control form working.
<input type="submit" value="send" id="send"/>
js:
$(document).ready(function() {
$('#send').click(function(e) {
e.preventDefault();
$('#name').addClass('error');
});
});
when you click the submit button, you send the form, thats why you should use e.preventDefault() .
You should also put your jquery code inside the document.ready function
The following code should do what you are trying to achieve. Here is a working js fiddle.
$("#send").on('click', function(e){
$("#name").addClass("error");
e.preventDefault();
});
Tips: why are you using native javascript function when using jQuery.
Instead of:
var name = document.getElementById('name');
you can simply do:
var name = $('#name');
Hope that helps.

Change inner text and class when submitting in a form? [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
Is there any way to change the inner text of tag when the form is subbmitting?
For example, I have this HTML code:
<form>
<button>Submit</button>
</form>
When I click to the button or press Enter key, the submit text will change to processing or something like that. A class will be added to the button element..etc.
What element do you want to change? You can identify it a number of ways. The most common ways are by ID or class
$('form').submit(function(){
$('#element_id').text('what_you_want_to_write');
$('#element_id').attr('class', 'new_class_name');
return false;
});
Or, you can change the class name by doing
$('#element_id').addClass('new_class_name');
you can add a handler to the submit event of the form like
$('form').submit(function(){
$(this).find('button').text('Processing');
//form submission logic goes here if any
})
Change html:
<form>
<button id="submitButton" onclick="changeText()">Submit</button>
</form>
Javascript:
<script>
function changeText()
{
document.getElementById("submitButton").innerHTML="Processing...";
}
</script>
Try this
code
$(document).ready(function () {
$('#submit').click(function (e) {
$(this).val("Processing....");
$(this).addClass('processing');
$('#frm1').submit();
});
});
html
<form id="frm1">
<input type="button" value="submit" id="submit"/>
</form>
css
.processing
{
background:blue;
color:white;
}

Categories