Function keyup executed even though its not called - javascript

I have an input called itemtitle. When I click on a button the element called counter increases by one. So the code bellow works for the first item. The counter div's innerHTML is 1 and when I write in the itemtitle input the text shows up on item 1.
if (document.getElementById("counter").innerHTML === "1"){
$("#itemtitle").keyup(function() {
$("#item1").html($("#itemtitle").val());
});
I click the button again and the counter innerHTML goes up to 2. I there have the following code.
if (document.getElementById("counter").innerHTML === "2"){
$("#itemtitle").keyup(function() {
$("#item2").html($("#itemtitle").val());
});
But when I write in the itemtitle input the text shows up on both item1 and item2. Even though the counter value is 2. How do I solve this? When counter is 1 I only want the text to go to item1. And when counter is 2 it should only go to item 2.
Thanks in advance

You are registering keyup event based on those two if conditions, So it would not mean that the particular events will fire only when the if condition matches, It would fire all the times. So what you should do is, Write a if condition inside that keyup event, so that the branching statement would react according to the html values. i.e] 1 or 2
Try this,
$("#itemtitle").keyup(function() {
if($.trim($("#counter").html()) === "1")
{
$("#item1").html($(this).val());
}
else
{
$("#item2").html($(this).val());
}
});

I solved it now. And were still able to have them seperated. I changed it to:
$("#itemtitle").keyup(function() {
if (document.getElementById("counter").innerHTML === "1"){
$("#item1").html($("#itemtitle").val());
}
});
This way it checks the value whenever i enter something into the input.

Related

How to execute an event with javascript only after a button is clicked or other event has happened?

So I have this function called check1 which executes when you click a specific submit button (last say this is button1). Now i want a new function to execute when you click another button (last say button2) that checks whether the first button(and event thats linked to it) is clicked/ excecuted.
Example:
function check1 () {
document.getElementById("id1").innerText= "hello";
}
button1.addEventlistener("click", check1);
button2.addEventlistener("click", check2);
(The code i'm asking about:)
function check2 () {
if (button 1 is clicked/ check1 is executed) {
document.getElementById("id2").innerText= "hello you";
} else {
document.getElementById("id2").innerText= "First click the other button";
My code is quite complicated and since im not a native english speaker, it will be hard for me to explane. That's why im using this simple example. I only want to know how you (and if you) can check in javascript if a specific other button is click beforehand / how you can check in javascript if a other function is already been executed.
I hope you guys can help me!
Your best option here is most likely to use custom attributes. You can then use Javascript to discern whether the custom attribute is added to the first button and if it's not present, then you can throw the error
document.getElementById("id1").setAttribute("clicked", "true");
This line will set the custom attribute on the button, then you can use the following to know if it it’s on the button
document.getElementById("id1").hasAttribute("clicked");
If you don't need to worry about people F12 hacking then I'd add a hidden input:
<input type="hidden" value="0" id="btn_CheckValue"/>
Then have an onclick="buttonClicked()" on the button
function buttonClicked(){
document.getElementById("btn_CheckValue").value = "1";
}
Then do your function checks on the second button.
function check2 () {
let btnValue = document.getElementById("btn_CheckValue").value
if (btnValue == 1) {
document.getElementById("id2").innerText= "hello you";
} else {
document.getElementById("id2").innerText= "First click the other button";
An example:
https://jsfiddle.net/tgm7avhb/51/

Click on button first time show alert, click second time execute code

I want to show users, when they click on a button for the first time, an alert when in a date field a value is chosen which lies before the current date. When they insist to this choice for good reasons, I want them to give a second chance to click on the same button, and then the value has to be submitted.
The click event is defined in a function:
$("#edit_date_btn").click(function(){
// do something and save
}
In an other function the comparing is handled. The basic code is:
function edit_date_compare() {
....
if(usersDate < today)
{ //show alert
return false; // needed for the first click so the input is not submitted
}
I've tried several options e.g. with a count on the click function (on a second click 'return true;' instead of 'return false;') but it seems difficult to handle this situation. Any ideas how to make this successful? It could be with Javascript or jQuery.
You have the right idea with the count on the click function. I suspect you might have difficulty implementing it. You need a separate variable that tracks the number of clicks. Here's a working fiddle: http://jsfiddle.net/zwmquxaL/
$(document).ready(function () {
var clickCount = 0;
$("#btnSave").click(function () {
if (clickCount > 0) {
AlertSave();
} else {
AlertFirst();
clickCount++;
}
});
});
function AlertSave() {
alert("Some code has been executed!");
}
function AlertFirst() {
alert("Are you sure you want to perform this operation?");
}

How to Capture changing value of textbox

I have a webpage with a small survey. I want to pre populate some of the answers based on user inputs to previous question.
In the below code, if value of id QR~QID3 depends upon value of QID1_Total. However after the page loaded and even if the condition is met the textbox is not populated with correct value.
.addOnload(function()
{
if(document.getElementById("QID1_Total").value>15) {
document.getElementById("QR~QID3").value = "Good";
}
else{
document.getElementById("QR~QID3").value = "Average";
}
});
$("#QID1_Total").on("input", function() {
//statements goes here
});
use of on("input" will track every inputting event, include drop and paste.
know more about onInput : https://mathiasbynens.be/notes/oninput
Here is an Fiddle Example to know how trigger works :
https://jsfiddle.net/5sotpa63/
An Assumption
Let Us Say you are using a function, which holds this statement show Good and Average according to users Input.
var targetElem = document.getElementById("QID1_Total");
var showComment = (targetElem,value>15) ? "Good" : "Average";
document.getElementById("QR~QID3").value = showComment;
Above code is the shorter method of your own statement mentioned in your question.
Now on Change of the target QR~QID3 you need to load some content. you utilize the below code as follows.
$("#QR~QID3").on("input", function() {
//your next question loading statements goes here,
//statements to proceed when you show some comment Good or Average
}).trigger("input");
Hope! this could be helpful.
$('#QID1_Total').keydown(function () {
//ur code
});
as the mouse key is pressed in the input field the function is called
You need to add an event listener to the "QID1_Total" element.
If you want to run the check while the user changes the input, i.e. after each keypress use the oninput event.
If you want to run the check after the user has completed the input, use the onchange event. The onchange event will only fire after the input loses focus.
You can bind the event listeners by using the addEventListener() function like this:
document.getElementById("QID1_Total").addEventListener("input", function(){
//Code goes here
});
Here is a JSFiddle showing both methods.
You also have to use the parseInt() function on the textbox values before you can perform mathematical functions with them.

Show alert when focusout, only once

I'm having a little problem with my script below. I'm trying to create a script where you can write some text in an input field, and when you have typed some text, you will get an alert when focus-out. The alert should only show, if the input contain text.
But the problem is, that if you're trying to write some text, delete it and then focus-out of the input, the alert do not show next time, when you actually have written something and then focus-out.
Right now, the alert function always will "disappear" when focus out, no matter if you have written any thing or not.
I have tried to add a var already_alert_me_once = true before the alert, and then put everything inside an: if(already_alert_me_once == false), but that didn't do the trick.
I have also tried to change $('#title').focusout(function() with $('#title').one('focusout', function() which almost did the trick.
Here is my current script:
// When focus on title
$('#title').focus(function () {
// Check if empty
if (!$(this).val()) {
$('#title').one('focusout', function () {
if ($(this).val()) {
alert("YEP");
}
});
}
});
..and Here's a Fiddle
So my question is; How to I do so the alert only appears when you have written something, and after that never again (unless you reload the page).
Hope you can understand what I mean.
Thanks - TheYaXxE
You can unbind the focus using:
$('#title').unbind('focus');
Working Fiddle
pretty much the easiest solution, no need for .one or focus events :)
$('#title').blur(function() {
if( $(this).val() ) {
alert('!!');
$(this).unbind('blur');
}
});
http://jsfiddle.net/T7tFd/5/
instead of putting the variable "already_alert_me_once" inside the function, make it a global variable. that way the scope will be maintained. you could also add an attribute to the #title that indicates that it has already been clicked, then check to see if that attribute exists before executing the code.

Multiple clicks required to complete two functions

I have some javascript that for some reason requires two consecutive clicks in order to function correctly. Here is the code for the link:
Select All
Now, here is the code for the function that is called with the onclick:
function select_all_com(){
$("[name=file_com_appeal].com-checkbox").each( function() {
$(this).attr('checked', true);
});
UpdateTotalFee();
}
and finally, here is the last bit of code:
function UpdateTotalFee(){
var AppealCount = 0;
$('input[name=file_com_appeal].com-checkbox').each(function(){
if( $(this).next().hasClass('checked') ){
AppealCount++; }
});
$('#AppealFeeTotal').text("$"+(AppealCount*140));
}
This final part is supposed to run the first time the link is clicked but some reason it does not run the first time, only the second time. Specifically what I mean is that the first click updates all of the checkboxes from off to on, but does not update the #AppealFeeTotal. A subsequent click of the Select All link when the checkboxes are already selected then causes the #AppealFeeTotal to update.
Any ideas why this might be requiring two clicks? I should also add that there is one line of code in particular that I am unsure about. I inherited the code from someone else, and I'm not sure of the reason why this is used:
if( $(this).next().hasClass('checked') ){
Thanks for any ideas you might have.
A few things, first attr('checked') is not the same as hasClass('checked') I suspect this is where your problem is. Your code does not add a "checked" class that I can see, but you're counting where that is the case. You should be using is:checked selector for this.
Second, if I read your code correctly, you're just counting checked checkboxes to get your total. You can do this more efficiently like this:
$(":checkbox").filter(':checked').length
Naturally you'll want to refine that selector (so it only counts specific checkboxes) but without more html, I can't help with that.
$(document).ready( function() { // this is a function which execute when document is ready in jQuery
var clicks = 0; // I am taking clicks variable so, to check whether the user have clicked for the first time
$("a").on("click", function() { // this is a function which execute when target anchor tag is clicked
clicks++; // now user have clicked the anchor tag so, i have to increase value to 1
if(clicks==1) { // this condition checks whether user have clicked the anchor tag for the first time? if yes, then execute the code
$("[name=file_com_appeal].com-checkbox").each( function() { // this is a each function which loops through all targets perform operations
$(this).attr('checked', true); // while looping through all targets this will set attribute or property "checked" to true means its checked
});
}
else if(clicks==2) { // this conditions check that whether anchor tag is clicked for second time
var AppealCount = 0;
$('input[name=file_com_appeal].com-checkbox').each(function(){
if( $(this).prop('checked') ){
AppealCount++;
}
});
$('#AppealFeeTotal').text("$"+(AppealCount*140));
clicks = 0; // set to zero because if user repeatedly clicks the anchor tag
}
});
});

Categories

Resources