Unwanted scroll after button click - javascript

I am not really sure what code to post here because I don't know what could cause this and the code for this page is quite long, there is still no live version.
Basically it's a form with a few panels, only 1 is visible, when you click next the user is moved to the next screen. What happens is that when you click next there is an unwanted scroll to the footer, any idea what could cause this? view gif below:
EDIT: This is the button code:
Next
and jQuery:
$('.btn-next').on('click', function(e){
if (!$("[name='amount_tobe_charged']").val()) {
alert('Please fill in amount');
} else if($("[name='amount_tobe_charged']").val() < 1) {
alert('Please enter amount');
} else {
var tab = $(this).closest('.tab-pane');
var nxt = $(tab).next();
var empty = $(tab).find('.required').filter(function(){
if($(this).attr('type') === 'email'){
return this.value.indexOf('#') < 0;
} else {
return $.trim(this.value) === '';
}
});
var error = $(empty[0]).attr('data-error') || 'Please fill in all fields';
if(nxt.attr('id') === 'finish-tab'){
submitForm(tab, nxt);
} else {
if(!empty.length) {
changeTab(tab, nxt);
} else {
alert(error);
}
}
}

You can see it happening in your gif. It's because you're hiding a column that comes before the form, therefore once the column does not exist. It will push you down. You can add a scroll to get around with issue with jQuery (or vanilla but I will only provide jQuery's smooth scroll). Either implement the below and remove the style="display: none;" that's causing it to shift.
$('html, body').animate({
scrollTop: $(".form-elem").offset().top
}, 2000);
You'll want to place this after the column is hidden in your JS code.

Related

How to synchronise ExtJS "checkboxes" (buttons) with Javascript/JQuery?

I am currently trying to synchronize two checkboxes on a page.
I need the checkboxes to be synchronized - to this end, I'm using a Tampermonkey userscript to pick up when one of them is clicked. However, I'm at a loss as to how to do it.
I believe they are not actually checkboxes, but ExtJS buttons that resemble checkboxes. I can't check whether they're checked with JQuery because of this: the checked value is appended to a class once the JS behind the button has run.
I have tried preventDefault and stopPropagation, but either I'm using it wrong or not understanding its' usage.
I'm not quite clever enough to just call the JS behind the box instead of an onclick event. Otherwise, that would solve my issue.
This is my code:
//Variables - "inputEl" is the actual button.
var srcFFR = "checkbox-1097";
var destFFR = "checkbox-1134";
var srcFFRb = "checkbox-1097-inputEl";
var destFFRb = "checkbox-1134-inputEl";
//This checks if they're synchronised on page load and syncs them with no user intervention.
var srcChk = document.getElementById(srcFFR).classList.contains('x-form-cb-checked');
var destChk = document.getElementById(destFFR).classList.contains('x-form-cb-checked');
if (srcChk == true || destChk == false) {
document.getElementById(destFFRb).click();
} else if (destChk == true || srcChk == false) {
document.getElementById(srcFFRb).click();
}
//This is where it listens for the click and attempts to synchronize the buttons.
$(document.getElementById(srcFFRb)).on('click', function(e) {
e.preventDefault();
if (document.getElementById(srcFFR).classList == document.getElementById(destFFR).classList) {
return false;
} else {
document.getElementById(destFFRb).click();
}
});
$(document.getElementById(destFFRb)).on('click', function(e) {
e.preventDefault();
if (document.getElementById(srcFFR).classList == document.getElementById(destFFR).classList) {
return false;
} else {
document.getElementById(srcFFRb).click();
}
});
I'm at a bit of a loss...any help would be greatly appreciated.
Figured it out - I was comparing class lists without singling out what I wanted to actually match.
My solution:
$(document.getElementById(srcFFRb)).on('click', function(){
if (document.getElementById(srcFFR).classList.contains('x-form-cb-checked')
== document.getElementById(destFFR).classList.contains('x-form-cb-checked')) {
return false;}
else {
document.getElementById(destFFRb).click();;
}});
$(document.getElementById(destFFRb)).on('click', function(){
if (document.getElementById(srcFFR).classList.contains('x-form-cb-checked')
== document.getElementById(destFFR).classList.contains('x-form-cb-checked')) {
return false;}
else {
document.getElementById(srcFFRb).click();;
}});

Slide next previous anchor for each photos

As you can see from the below JSFIDDLE link that the next and previous functions are working fine. It is taking the user to the next or previous photos accordingly. The issue what i am facing is when user reach to the very last photo by clicking next button and then if again he press "next" button then the function is getting freeze and doesn't work anymore until refresh the page again. This is happening for the first image also that user is on the first image and if clicks the "previous" button then the page gets freeze and the function is not working anymore. Kindly check the demo and any suggestions that how can i fix the issue?
https://jsfiddle.net/jpex4908/1/
var $jq = jQuery.noConflict();
$jq(document).ready(function($jq) {
var scrollTo = function(yoza) {
$jq('html, body').animate({
scrollTop: $jq(yoza).offset().top
}, 300);
};
$jq('.next').click(function(event) {
event.preventDefault();
var $jqcurrent = $jq('.home-block > .current');
if ($jqcurrent.index() != $jq('.home-block > div').length - 1) {
$jqcurrent.removeClass('current').next().addClass('current');
scrollTo($jqcurrent.next());
}
});
$jq('.prev').click(function(event) {
event.preventDefault();
var $jqcurrent = $jq('.home-block > .current');
if (!$jqcurrent.index() == 0) {
$jqcurrent.removeClass('current').prev().addClass('current');
scrollTo($jqcurrent.prev());
}
});
});

Why when I press GO in iPad, does not focus next input?

Imagine that I have many div in display: none; and just the first one is in visible.
In the view, you can navigate through each one pressing a ENTER or GO (in an iPad). So when you want to advance, change the current div to none, and the next one change it to visible. But right now I wanna also set the focus in an input element where has a focusable class. But it does not set the focus.
Here is my code:
var setFocus = function () {
$("#question-container2").find("#question:visible").find('.focusable')[0].focus();
$("#question-container2").find("#question:visible").find('.focusable')[0].setSelectionRange(0, 0);
};
var nextPage = function () {
if ($currentPage < $totalPages) {
$currentPage++;
$("#question-container2").find("#question:visible").hide().next().show();
}
};
$(".input-area").keypress(function (e) {
if (e.keyCode == 13) {
// Do something
nextPage();
setFocus();
return false;
}
});
Specify a tab index in your elements. This is a browser accessibility attribute which will tell the browser where to navigate to next when you are done with the current field.
See: http://reference.sitepoint.com/html/a/tabindex

make use of the enter key

I have a list of urls(div) underneath an input field(div). I need the ability to come down in the list and than by hitting enter this will trigger some function designated to the url. This is the fidle: the script
Over the last days I have tried to much things to explain, but in conclusion none of it worked. Help would be appreciated.
After this line of code :
// set timers to do automatic hash checking and suggestions checking
setInterval(checkHash,500);
setInterval(checkSuggest,500);
Insert this :
$('#searchbox').keyup(
function (e){
var curr = $('#suggest').find('.current');
if (e.keyCode == 40)
{
if(curr.length)
{
$(curr).attr('class', 'display_box');
$(curr).next().attr('class', 'display_box current');
}
else{
$('#suggest li:first-child').attr('class', 'display_box current');
}
}
if(e.keyCode==38)
{
if(curr.length)
{
$(curr).attr('class', 'display_box');
$(curr).prev().attr('class', 'display_box current');
}
else{
$('#suggest li:last-child').attr('class', 'display_box current');
}
}
if(e.keyCode==13)
{
var search_terms = $('.current a').text();
// perform a search with this text...
doSearch(search_terms,true,false);
//update the search textbox
$('#searchbox').val(search_terms);
}
})
And don't forget to delete the previous code at the bottom...

Problem with event.target in IE

I'm writing js for a status update system to be used on various pages throughout a app that I'm working. I am really just starting to get more comfortable with javascript so it has been somewhat of a challenge to get to the point where I have everything now.
The status system is basically a facebook clone. For the most part everything is supposed to function the way that facebook's status updates and status comments do. The intended behavior is that when the user clicks in the status textarea, the div under the status textarea slides out revealing the submit button as well as some other checkboxes.
If the user clicks anywhere else on the page except a link or any element that has the class prevent_slideup the div slides up hiding the submit button and any checkboxes.
I'm using a document.body click function to determine what the user clicked on so I know which form elements to hide if I should even hide them. I do not want this slideup to take place on a textarea if that textarea has focus or the user is selecting a checkbox that goes with that form. Hence the prevent_slideup class. I also do not want to bother running the slideup logic if the user has clicked on a link. I'd prefer they just leave the page without having to wait for the animation.
The code that I was using to accomplish this task can be found in the $(document.body).click(function (e) section below where I'm doing a .is('a') check on the event target.
This code works as expected in chrome and firefox, however in ie when a link is clicked for the first time it seems that the element stored in var target is actually a div instead of an anchor. What ends up happening is that the submit div slides up and the user is not taken to the link that they just clicked on. If a link is clicked a second time the user is taken to the page as you would expect.
It seems to me that there's some kind of a lag in ie as to what the current event being fired is.
The entire status module is working other than this one strange ie bug regarding the users click on the link not being carried out the first time that they click a link after opening the status textarea. Does anything jump out in this script that would explain this behavior or does anyone have any other advice?
Thanks in advance for your help.
$(document).ready(function(){
$("textarea.autoresize").autoResize();
});
$(document.body).click(function (e){
var target = e.target || e.srcElement;
console.log(target);
console.log($(target).is('a'));
if($(target).hasClass('prevent_slideup') || $(target).is('a'))
{
return true;
}
else
{
var active_element = document.activeElement;
var active_status_id = $(active_element).attr('data-status_id');
var active_has_data_status_id = (typeof active_status_id !== 'undefined' && active_status_id !== false) ? true : false;
$('textarea').each(function(){
if($(this).hasClass('status_comment_textarea'))
{
var status_id = $(this).attr('data-status_id');
if($('#comment_textarea_'+status_id).val() === '' && (!active_has_data_status_id || active_status_id !== status_id))
{
hide_status_comment_submit(status_id);
}
}
else if($(this).attr('id') === 'status_textarea')
{
if($('#status_textarea').val() === '' && $(active_element).attr('id') !== 'status_textarea')
{
$('#status_textarea').html($("#status_textarea").attr('placeholder'));
hide_status_submit();
}
}
});
return true;
}
});
$("#status_textarea").live('click', function(){
if($('#status_textarea').val() === $("#status_textarea").attr('placeholder'))
{
$('#status_textarea').html('');
}
show_status_submit();
return false;
});
$(".comment_toggle").live('click', function(){
var status_id = $(this).attr('data-status_id');
show_status_comment_submit(status_id);
return false;
});
$(".status_comment_submit").live('click', function(){
var status_id = $(this).attr('data-status_id');
$('#status_comment_submit_wrapper_'+status_id).addClass('status_comment_submit_successful');
return false;
});
$(".show_hidden_comments").live('click', function(){
var status_id = $(this).attr('data-status_id');
$('#status_hidden_comments_'+status_id).show();
$(this).hide();
return false;
});
function hide_status_submit()
{
$("#status_textarea").removeAttr('style');
$("#status_textarea").blur();
$("#status_block").removeClass('padding_b10');
$("#status_submit_wrapper").slideUp("fast");
return false;
}
function show_status_submit()
{
if ($("#status_submit_wrapper").is(":hidden"))
{
$("#status_block").addClass('padding_b10');
$("#status_submit_wrapper").slideDown('fast');
}
return false;
}
function hide_status_comment_submit(status_id)
{
if(!$('#status_comment_submit_wrapper_'+status_id).is(":hidden"))
{
$('#status_comment_submit_wrapper_'+status_id).hide();
$('#fake_comment_input_'+status_id).show();
$('#comment_textarea_'+status_id).removeAttr('style');
}
return false;
}
function show_status_comment_submit(status_id)
{
if($('#status_comment_submit_wrapper_'+status_id).is(":hidden"))
{
$('#fake_comment_input_'+status_id).hide();
$('#status_comment_submit_wrapper_'+status_id).show();
$('#comment_textarea_'+status_id).focus();
}
return false;
}
function status_comment_submit_successful()
{
hide_status_comment_submit($('.status_comment_submit_successful').attr('data-status_id'));
$('.status_comment_submit_successful').removeClass('status_comment_submit_successful');
return false;
}
I figured out that there were two main issues with my script...
1.) The document.body function and the #status_textarea live click funtioins were conflicting with each other.
2.) After adding the logic for the #status_textarea function into the document.body function I noticed that the script still didn't quite work as expected in internet explorer unless I had an alert in the function. The problem at this point was that the autoresize plugin that I'm using on the textarea was also conflicting with the document.body function.
I was able to rectify the situation by adding a dummy text input and hiding the status textarea. On click of the dummy text input the status textarea is shown and the the dummy text input is hidden. I have no idea why this worked, but it seems to have solved my problems.

Categories