DirtyForms does not work on input hidden field - javascript

I added dirtyForms to my forms to detect any changes on one of the input fields https://github.com/snikch/jquery.dirtyforms
HTML
<form>
<input type="text" id="post" name="post">
<input type="hidden" id="body" name="body">
<froala-editor input="body">
</froala-editor>
</form>
Javascript
$('document').ready(function() {
$('form').dirtyForms();
});
However for input hidden seems like it doesn't add the dirty class, it only works for input type="text" . Any ideas on how to solve this problem?

Because it does not make sense when the user is not the one entering the data in the field.
You can do it yourself
SO does not allow the editor so I tested here
$('form').toggleClass('mydirty', e.target.textContent !== "");
tests the editor, it makes more sense than the input field
https://plungjan.name/SO/froala/
$('form').dirtyForms({
helpers: [{
isDirty: function($node, index) {
if ($node.is('form')) {
return $node.hasClass('mydirty');
}
}
}]
});
var editor = new FroalaEditor('#froala')
$(document).on("keyup", function(e) {
$('form').toggleClass('mydirty', e.target.textContent !== "");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.dirtyforms/2.0.0/jquery.dirtyforms.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/froala-editor#3.2.3/js/froala_editor.min.js"></script>
<form>
<input type="text" id="post" name="post">
<input type="hidden" id="body" name="body">
<div id="froala" input="body">
</div>
</form>

Related

How to greet with jQuery?

I want to do this by typing my name in the input, then pressing the button and an h1 tag will appear below the input: Hello (my name)! I don't know how to solve this.
Here is my html and jQuery code:
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#button').click(function () {
$('body').append('<h1></h1>')
$("h1").append('Hello')//+ my name
})
</script>
You need to get the value from the #Name input. Unless you have a specific reason for doing this in multiple steps, you can append the html as a whole like this:
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$('#button').click(function () {
if ($('#Name').val()) { //make sure the user entered something
$('body').append(`<h1>Hello ${$('#Name').val()}!</h1>`);
}
});
</script>
The backticks `` are a template literal, they allow you to build strings without a bunch of concatenation.
`Hello ${my_name}!` vs 'Hello ' + my_name + '!'
To get the value of the input field you can use:
$('input#Name').val();
Instead of appending it to the h1 element, you can simply set its text to the wanted value, in your case: .text('hello '+getName).
In case a visitor makes a mistake you only need to modify the inner text of the h1 instead of appending it to the body again.
$('h1').text();
$('#button').click(function () {
let getName = $('input#Name').val();
if(!$('h1').length){
$('body').append('<h1 class="greeting-title"></h1>');
}
$("h1").text('Hello '+getName)//+ my name
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<label for="Name">Name</label>
<input type="text" id="Name">
<button id="button" type="button">Click</button>
</form>

Inserting <object> in current html form

I have a webpage with an input box and a button. If a user inputs some values (12345) an object need to appear below or instead of that form (the input box and the button).
I am handling a value check through and my whole code looks like this:
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc() {
if (document.getElementById("subject").value == 12345) {
document.write(
"<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>"
);
}
}
</script>
Currently this code is showing a object in a new window (only a object on the page).
Also this is offtopic but if you can help, how can I handle if enter is pressed to activate function proveriKljuc()?
Don't use document.write. If you want to replace the current form:
<div id="wrapper">
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>
If you want your object to appear under the form:
<div id="wrapper">
<form name="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
</form>
</div>
<button onclick="proveriKljuc()" style="margin-top:20px">Potvrdi!</button>
<script>
function proveriKljuc()
{
if(document.getElementById("subject").value == 12345)
{
document.getElementById("wrapper").innerHTML += "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
}
</script>
You can NEVER use document.write after page load. It will wipe the page. Instead have div or a span and fill the innerHTML of it or appendChild of a div created using document.createElement. return false onsubmit to stop submission or make the button type="button" - alternatively show a hidden div How to show hidden div after hitting submit button in form?
I cannot show a working example because stacksnippets do not support submit
window.onload = function() {
document.getElementById("form").onsubmit = function() {
if (this.elements["subject"].value == 12345) {
document.getElementById("objContainer").innerHTML = "<center><object id='object1' data='http://termodom.rs/wp-content/uploads/2016/12/akcija1-1.jpg'></object></center>";
}
return false; // ignore submit
}
}
<form id="form" action="" method="post">
<input type="text" name="subject" id="subject" placeholder="UNESI KOD">
<button type="submit" style="margin-top:20px">Potvrdi!</button>
</form>
<div id="objContainer"></div>

Why is "enter" not working for my form?

Here is the code, I can't figure out why enter/return isn't working! Is it because it's inline?
HTML
<div class="wrap"><form name="login" style="margin: 0px">
<label for="fname">CLICK TO ENTER PASSWORD</label>
<input TYPE="text" NAME="pass" size="17" onKeyDown="e.keyCode == 13;" id="fname" class="cool"><br><input type="button" value="LOGIN" class="asbestos-flat-button" onClick="TheLogin(this.form)">
</form>
</div>
JS
<script language="JavaScript" type="text/javascript">
<!--- PASSWORD PROTECTION SCRIPT
function TheLogin() {
var password = 'password';
if (this.document.login.pass.value == password) {
top.location.href="home.html";
}
else {
location.href="index.html";
}
}
// End hiding --->
</script>
I'm learning JS so any help would be so awesome!
UPDATE
Thanks for your help. Still not working when integrated. The page doesn't load the home.html when I hit enter/return. Instead I get no refresh, and the address bar has the url http://example.com/?pass=password.
If I click the button it does load the home.html!
thanks!
Here I wrote a JSFiddle with the working example.
In the HTML code:
Remove onKeyDown="e.keyCode == 13;" from the <input> text element.
Remove onClick="TheLogin(this.form)" from the <input> button element.
Change the type of input button from 'button' to 'submit'. In this way, when you press "enter" in the input text form the form is submitted.
Intercept the "submit" event in the form, adding onSubmit="theLogin(this.form)" on <form> element.
Note: I have renamed the function name from "TheLogin" to "theLogin" because in JavaScript the functions begins with lowercase letters if they are not constructors.
The HTML code:
<div class="wrap">
<form name="login" style="margin: 0px" onSubmit="theLogin(this.form)">
<label for="fname">CLICK TO ENTER PASSWORD</label>
<INPUT TYPE="text" NAME="pass" size="17" id="fname" class="cool">
<br>
<input type="submit" value="LOGIN" class="asbestos-flat-button">
</form>
</div>
And the JavaScript code:
theLogin = function() {
var password = 'password';
if (this.document.login.pass.value === password) {
top.location.href = "home.html";
} else {
location.href = "index.html";
}
}
You have missed the <input type="submit">, without it you can't use the Enter key to submit the form.

add name= required with js (eForm Modx)

I use Modx eForm for my form.
My problem, I need to add :required to a couple of input fields but only when the checkbox is unchecked!
I have fields like this:
<input type="text" placeholder="Last name" id="deliver_lastname" name="deliver_lastname:required" class="[[!+fi.error.deliver_lastname:notempty=`error`]]" />
:required needs to be set after unchecking the checkbox.
I have a checkbox to default hide some fields.
<p id="shiptobilling" class="form-row">
Same as billing address <input type="checkbox" onclick="SetBilling(this.checked);" checked="checked" />
</p>
A little script to hide the div with fields into it:
<script type="text/javascript">
function SetBilling(checked) {
if (checked) {document.getElementById('deliveryaddres').style.display="none";}
else {document.getElementById('deliveryaddres').style.display="block";}
}
</script>
Can be made easier - let deliveryaddres will always be required, and when checkbox is checked just let the value of the field is synchronized with billingaddres.
Needed javascript:
<script type="text/javascript">
function SetBilling() {
var deliveryaddres = document.getElementById('deliveryaddres');
var billingaddres = document.getElementById('billingaddres');
var shiptobilling = document.getElementById('shiptobilling').getElementsByTagName('input')[0];
if (shiptobilling.checked) {
deliveryaddres.value = billingaddres.value;
}
}
</script>
Needed events "onclick" and "onchange" on inputs:
<input type="checkbox" id="deliveryaddres" onclick="SetBilling();" />
<input type="text" id="billingaddres" onchange="SetBilling();" />
Add something like
document.getElementById("deliver_lastname").required = true;

How to manipulate Get query?

I have the following form from http://regain.sourceforge.net/:
<form name="search" action="search.jsp" method="get">
<p class="searchinput">
<b>Suchen nach: </b>
<input name="query" size="30"/>
<select name="order" size="1" ><option selected value="relevance_desc">Relevanz</option><option value="last-modified_asc">Dokumentendatum aufsteigend</option><option value="last-modified_desc">Dokumentendatum absteigend</option</select>
<input type="submit" value="Suchen"/>
</p>
</form>
the search form works fine. The URL looks like the following:
http://localhost:8080/regain/search.jsp?query=queryfieldvalue&order=relevance_desc
Now I want to add a checkbox to manipulate the value of the input field query.
If the checkbox is checked then the query value should look like filename:"queryfieldvalue"
http://localhost:8080/regain/search.jsp?query=filename%3A%22queryfieldvalue%22&order=relevance_desc
What's the best way to do this? Javascript? Do you have a short example for me because I'm really new to javascript.
Thanks a lot in advance.
one way with pure javascript (without jquery) would be
<script type="text/javascript">
function handler()
{
var check = document.getElementById('check');
var query = document.getElementsByName('query')[0];
if(check.checked)
{
query.value = "filename:\"" + query.value + "\"";
}
else
{
query.value = query.value.replace(/^filename:"/, "").replace(/"$/, "");
}
}
</script>
<form>
<input type="text" name="query" />
<input type="checkbox" id="check" onclick="handler()" />box
</form>
it should more or less work, it would be safer if you give query input field an id and then reference it by id, not name
if you use jQuery, something like this should do:
<input type="checkbox" id="chkQuery">Pass queryfield</input>
<script>
$(document).ready(function{}
$("#chkQuery").click(function(){
if ($(this).is(':checked'))
$("input[name='query']").val("filename:queryfieldvalue");
else
$("input[name='query']").val("queryfieldvalue");
});
});
</script>

Categories

Resources