Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent label page scrolling to input

Anybody know of a way to prevent the page jumping to an input when is is changed? Particularly if it's changed with a label button from elsewhere on the page.

This problem affects IE, Chrome Canary, FireFox, and probably some others, in-fact the only browser it doesn't seem to affect is Chrome v28.

You can see the problem here: http://jsfiddle.net/FgaWM/3/

Normally this would be a useful feature, but in my case it is quite annoying, I need a way to prevent it / override it.

I've tried forcing the scroll position with JQuery:

var labelPos = $(window).scrollTop();
$(window).scrollTop(labelPos);

This solution is... buggy at best, causing momentary page flicker.

Anyone have a better way?

Edit: I figured-out a solution :P

$('label').click(function(e) {
    e.preventDefault();
    var For = $(this).attr('for');
    $('#' + For).trigger('click');
});
like image 436
PlasmaDan Avatar asked Jul 19 '13 17:07

PlasmaDan


People also ask

How do I stop web pages from scrolling?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.

How do you make a scrollable label?

Place a panel in location where you want the label to be, set it's AutoScroll property to true. Then place the label in the panel, anchor it and set it's AutoSize property to true. This will make the panel provide the scroll bars if the label's text extends outside of the panel.


1 Answers

$('label[for]').on('click', function (e) {
    var target = window[this.htmlFor];
    target.checked = !target.checked;
    e.preventDefault();
});
like image 181
A. Wolff Avatar answered Sep 18 '22 15:09

A. Wolff