I think this has been asked already, I have an iframe page where we have nearly 10 textfield's, whenever the user taps on any one of the textfield, I would like to know the Y position in that page. Is that possible?
Asked
Active
Viewed 4,190 times
1
-
1Is the content within the iframe on the same domain? Secondly, what have you tried already? Can you provide any examples? – Lewis Apr 12 '16 at 16:18
-
Are both files on the same server/domain? – Alessandro Bassi Apr 12 '16 at 16:19
-
@Lewis, yes it is in the same domain. I was trying in the actual code, didn't have any example to post it here, sorry. – user2431170 Apr 12 '16 at 16:21
-
I tried these links http://stackoverflow.com/questions/5007530/how-do-i-scroll-to-an-element-using-javascript – user2431170 Apr 12 '16 at 16:22
-
http://stackoverflow.com/questions/11880443/how-to-scroll-browser-to-desired-element-by-javascript – user2431170 Apr 12 '16 at 16:22
2 Answers
1
This is untested, but should point you in the right direction.
jQuery
$(document).ready(function(){
var iframe = $('iframe');
var inputs = iframe.find('input');
$.each(inputs, function(){
$(this).click(function(){
console.log($(this).offset().top));
})
})
})
Vanilla Javascript
Updated as per comment. Also untested.
Give your iframe an ID to make it easier to target, then:
var iframe = document.getElementById('iframe');
var inputs = Array.prototype.slice.call(iframe.getElementsByTagName('input'));
inputs.forEach(function(input,i){
input.addEventListener('click', function(){
console.log(this.getBoundingClientRect().top;
})
})

Lewis
- 3,479
- 25
- 40
0
If both, the main window and the iframe content are within the same protocol/domain, then you can directly access the iframe's contentBody and get the scrollTop position of the input field.
For instance:
var y = $('iframe').contents().find('#myInputField').offset().top;

Raul Fernandez
- 118
- 7