What cause to button working like a submit button? - html

I have tow buttons inside my form,
one is a submit and the second is a simple button (like cancel button that close the dialog).
Code:
<dialog id="addSubjectDialog" style="width:20%; height:25%;">
<div style="width:100%; hegit:20%;"><h2 style="font-size:100%;"><fmt:message key="add_subject_dialog_title" /></h2></div>
<form action="addSubject" method="post" >
<div style="width:100%; hegit:20%;">
<h2 style="font-size:100%;"><input id="SubjectName" name="SubjectName" style="font-size:100%;text-align: right;"></input> <fmt:message key="insert_subject_name" /> </h2>
</div>
<div style="height: 60px;margin-top:5% ;">
<fmt:message key="save" var="save"/>
<input type="submit" class="btn btn-success" style=" float: left; width:40%; "value="${save}"></input>
<button id="cancelSubSubjectDialog" class="btn btn-warning" style="float: right; width: 40%;" ><fmt:message key="cancel" /></button>
</form>
</div>
</dialog>
the code of the cancel button is in a script:
<script type="text/javascript">
window.onload = function () {
document.getElementById("cancelSubjectDialog").addEventListener('click', function(e) {
e.preventDefault();
document.getElementById("addSubjectDialog").close();
});
document.getElementById("saveSubjectDialog").addEventListener('click', function(e) {
e.preventDefault();
document.getElementById("addSubjectDialog").close();
});
}
function ShowDialog(dialogName) {
selectList();
document.getElementById(dialogName).showModal();
}
</script>
In the onLoad function I set the cancel button action.
*I have another dialog in my page that do the same things and include cancel and save buttons too, and it's working fine.
When I click on the cancel button the "addSubject" is called;
What is wrong in my code? Why does the action is called if I click on cancel button too?

Related

How to select a box with jQuery but not select the form inside the box?

I have a code where I want the page to close when the box is clicked with the "box-search" class
But even though I said don't consider the form and the elements inside the form, the form will be closed when the form and input are clicked.
$(".box-search *").not('form, form *').click(function() {
$(".box-search").fadeOut();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box-search">
<div class="container" style="font-size: 40px; color: red;">
<button type="submit" class="close-box-search">Close</button>
<form method="get" action="">
<input type="text">
<button type="submit">Search</button>
</form>
</div>
</div>
I found the answer to my question
To select the parent, but not apply to the child
We use the following code
$(".box-search").click(function (e) {
if( $(e.target).hasClass("box-search") || $(e.target).hasClass("container") || $(e.target).hasClass("close-box-search") ){
hideBoxSearch()
}
});

run click function before submiting default again in jquery

im trying to prevent the default action on a button then run my function and after my function is done then run default again, however i dont know how to build my jquery correctly.
here is what i have
/* somewhere else .with--abo class does something that prevents my click function from working */
/* so now when ever i click it i prevent it from running default before my click function */
$(".with-abo").click(function(event) {
event.preventDefault()
$("#sure").click();
$(".table--add-product").submit();
});
$("#sure").click(function(){
$("#confirm").toggleClass("is--hidden");
$("#twpConfirm").click();
});
$("#twpConfirm").click(function(){
alert("deleted")
});
#confirm{
color:red;
}
.is--hidden {
display: none !important;
}
#sure{
margin-top: 10px;
background-color:red;
width:40px;
border: 1px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post" title="Sdsd" class="table--add-product add-product--form block-group">
<input type="hidden" name="twpAboAdd" value="1">
<button type="submit" class="btn with-abo is--primary block is--icon-right">
add item to subscription
<i class="icon--arrow-right"></i>
</button>
<input type="hidden" name="__csrf_token" value="iSTHZQdHHpP5iCcNM0u6NvPPHroipp"></form>
<div id="sure" class="btn is--small column--actions-link" title="">
<i class="icon--cross">delete item</i>
</div>
<div class="panel--td column--actions">
<div id="sure" class="btn is--small column--actions-link" title="">
<i class="icon--cross"></i>
</div>
<div id="confirm" class="is--hidden">
<span>
are you sure?
<button id="twpConfirm" type="submit" class="btn column--actions-link">
<i class="icon--check"></i>delete
<input type="hidden" name="noSurcharge" value="1">
</button>
</span>
</div>
<input type="hidden" name="__csrf_token" value="iSTHZQdHHpP5iCcNM0u6NvPPHroipp">
</div>
basically when i click [add item to subscription] i want to automatically click the [delete] button. but how my function is built i trigger the click and then run default again before my trigger function is done.
i was hoping that something like this would work:
$(".with-abo").click(function(event) {
event.preventDefault()
$("#sure").trigger("click", function(){
$("#confirm").toggleClass("is--hidden");
$("#twpConfirm").click();
});
$(".table--add-product").submit();
});
but this is so wrong and dont know how go around it. any suggestion is appreciated.

Jquery UI dialog form button type change to submit

I have Ui dialog form with jquery and its convert Submit and cancel buttons which is [type="button'] both of them. I would like to change type of submit button to the "submit".
How can I do it? im failed all of my tries
This is js codes.
$('#jui-dialog-form-horizontal').dialog({
autoOpen: false,
modal: true,
width: 700,
buttons:
{
Submit: function () {
$(this).dialog('close');
},
Cancel: function () {
$(this).dialog('close');
}
}
});
This is output to html.
<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<div class="ui-dialog-buttonset">
<button type="button" class="ui-button ui-corner-all ui-widget">Submit</button>
<button type="button" class="ui-button ui-corner-all ui-widget">Cancel</button>
</div>
Consider the following example.
$(function() {
$('#jui-dialog-form-horizontal').dialog({
autoOpen: false,
modal: true,
width: 700,
buttons: {
Submit: function() {
$(this).dialog('close');
$("#jui-form").submit();
},
Cancel: function() {
$(this).dialog('close');
}
}
});
$("#jui-form-save").click(function(event) {
event.preventDefault();
$('#jui-dialog-form-horizontal').dialog("open");
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<form id="jui-form">
<label>User Name</label>
<input type="text" name="username" id="username" />
<button type="submit" id="jui-form-save">Save</button>
</form>
<div id="jui-dialog-form-horizontal">
<p>Are you sure you want to Submit this data?</p>
</div>
This example shows you how you can trigger the submit event on the form when the User clicks the Submit Button in the Dialog.

Why form with few inputs in it, without any buttons, doesn't submit?

Why form with only one text input (without any buttons) will submit, but with two text inputs won't?
Example: https://codesandbox.io/s/form-submitting-ztqpz?fontsize=14
Add a submit button on each form (few inputs too) and it will work both with button and pressing enter
<div class="panel">
<div class="panel-title">Few inputs</div>
<form action=".">
<input type="text" />
<input type="text" />
<button type="submit">Click</button>
<output name="result" />
</form>
</div>
</div>
you can do it in many ways,
best one is with tabindex to prevent tab reach this button -
<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />
Also you can hide your button link this. or you can go with java script function like this -
<script type="text/javascript">
// Using jQuery.
$(function() {
$('form').each(function() {
$(this).find('input').keypress(function(e) {
// Enter pressed?
if(e.which == 10 || e.which == 13) {
this.form.submit();
}
});
$(this).find('input[type=submit]').hide();
});
});
</script>

How to refresh only div on submit button click?

I have 2 div in my web page. what I need is submit only form_div when press the submit button.
<div id="main">
<div id="form_div">
<form name="form">
<input type="submit" name="save" id="save"/>
</form>
</div>
<div id="other_div"></div>
</div>`
In jquery,
you could call
$("your-custom-selector").click(function(){
//Do-whatever. This code only runs when your customselector is pressed.
});
In JavaScript
document.getElementById("your-custom-selector").addEventListener("click", myFunction);
function myFunction() {
//Do-whatever. This code only runs when your customselector is pressed.
}