-2

Is there a way to change this code to target a div with an ID?

onload="javascript:parent.scrollTo(0,0);"  

I am pulling my hair out trying to figure this out. This is related to my question here but I can't reply to that to get any help.

Edit: My apologies. I am new to Stack Overflow and in my other post I give a lot of information. When I wanted to update it with more information/questions because I have been spending hours and hours trying to figure this out, I could not as it does not give me the option. So yes it is "related" to my other question however there are no replies for me to add more information. I guess I am not allowed to post any updated information or questions.

Community
  • 1
  • 1
Maquar
  • 38
  • 6
  • 2
    Please, for god's sake, don't put `javascript:` in `onload`! – Derek 朕會功夫 Dec 19 '15 at 23:24
  • This is not my code. I am using a form script and trying to get it to do what I want (actually what it should be doing anyways). I know nothing about javascript. You can see my link to my issue if you want an explanation. Thank you for your reply. – Maquar Dec 19 '15 at 23:29
  • 1
    Not sure why don't you use: `onload="document.getElementById('myDIV').scrollIntoView();"` ??? – A. Wolff Dec 19 '15 at 23:33
  • Possible duplicate of [Scrolling to an ID when a form is submitted?](http://stackoverflow.com/questions/34367012/scrolling-to-an-id-when-a-form-is-submitted) – MortenMoulder Dec 19 '15 at 23:42
  • Boy was I excited to try this. Unfortunately it made my form disappear when I changed `onload="javascript:parent.scrollTo(0,0);"` to `onload="document.getElementById('#mf_placeholder').scrollIntoView();"`. I am dealing with a form script with lots of files (not written by me) and I have contacted the devs but have not heard a response so i am attempting to try and figure it out. Thank you for your reply A Wolff – Maquar Dec 19 '15 at 23:43
  • @Snorlax - Is it considered a duplicate? My other post that you linked to doesn't give me an option to update it with other information or questions. – Maquar Dec 19 '15 at 23:46

1 Answers1

2
function scrollTo(element) {
    location.hash = element;
}

https://jsfiddle.net/43j177x9/1

Call it like this:

scrollTo("element");

That will cause it to scroll down to your <div id="element"></div> element.

In your case, it should be:

onload="scrollTo('element');"

Alternatively you can use document.getElementById('element').scrollIntoView(); (thanks to Derek 朕會功夫).

Community
  • 1
  • 1
MortenMoulder
  • 6,138
  • 11
  • 60
  • 116
  • Thanks for your input Snorlax but then this automatically scrolls to the ID on page load. I am seeing this is a lot more complicated than what I thought it was going to be. The script I am using has the code I pasted above in it but somehow the code must be triggered or something. My link I gave gives a lot more detail. I appreciate your attempt at helping me though. – Maquar Dec 19 '15 at 23:34
  • 1
    @Maquar I simply answered your question based on your code. If my answer has not solved your problem, you should update your post with some more information. – MortenMoulder Dec 19 '15 at 23:35
  • 1
    There's actually a native function for that `.scrollIntoView` – Derek 朕會功夫 Dec 19 '15 at 23:35
  • @Derek朕會功夫 You are absolutely correct. I did not know about that. I have updated my post. – MortenMoulder Dec 19 '15 at 23:38
  • @Snorlax You certainly did Snorlax and my apologies. I am new to Stack Overflow so i didn't want to repost my entire other thread which is located at [link](http://stackoverflow.com/questions/34367012/scrolling-to-an-id-when-a-form-is-submitted) – Maquar Dec 19 '15 at 23:40