tags:

views:

85

answers:

5

I have a PHP form for discussions. Each message has its own response button, which is dynamically generated. I'm using javascript in the button to make a response form visible at the bottom of the page, but I can't for the life of me get the page to jump down to the form once it's visible. This is a problem for pages that have a lot of discussions on it, as some users may not know to scroll down and will just think the button didn't work.

Here's what I have now for my button code:

<a href="#" onClick="changeVisibility(3,'responseForm')"><img src="images/reply.jpg" border=0 /></a>

The changeVisibility function looks like this:

function changeVisibility(parentID, elementID) {
  document.getElementById(elementID).style.visibility="visible";
  document.forms[0].parent_id.value=parentID;
  var el = document.getElementById(elementID);
  el.scrollIntoView(true);
}

In my form, I have a div whose id is set to responseForm. When clicking the button, the div does become visible, but the scrollIntoView is not working - I have to manually scroll down to see it. Any ideas?

+3  A: 

Use window.location.hash

function changeVisibility(parentID, elementID) {
  document.getElementById(elementID).style.visibility="visible";
  document.forms[0].parent_id.value=parentID;
  window.location.hash = '#' + elementID;
  return false;
}

<a href="#" onClick="return changeVisibility(3,'responseForm')"><img src="images/reply.jpg" border=0 /></a> 

EDIT: I think the issue before was that you weren't returning false, so the default action (going to #) was still occurring.

Matthew Flaschen
Nope, still does the same thing my original code did - kind of flashes for a split second, then goes right back to the original display of messages. I've tested in Firefox 3.6.3, Chrome, and IE8.
EmmyS
A: 

User window.location.hash to redirect to an ID/anchor. E.g.

HTML:

<p id="youranchor">bla bla</p>

JavaScript:

window.location.hash='youranchor';

EmmyS - This code does work. Here's a complete example for you:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html>
  <head>
    <title>Some title</title>

    <script type="text/javascript">
      function jumpToParagraph() {
        window.location.hash='paragraphjump';
      }
    </script>
  </head>
  <body>
    <p onclick='jumpToParagraph();'>Jump to the paragraph at the end! [version 1]</p>

    <p><a href="javascript:jumpToParagraph();">Jump to the paragraph at the end! [version 2]</a></p>

    <p style="height: 1500px;">Some nonsense</p>

    <p id="paragraphjump">You made the jump</p>
  </body>
</html>

Put it into a file and test the file in your browser.

Gert G
Same code as listed in first answer above; doesn't do anything.
EmmyS
OK, just tried it again, and here's what's strange (or maybe not; it's been forever since I worked with CSS and javascript): if I put the onClick event in a paragraph tag, it works fine. If I try to put it in an href tag (as in a href="#" onClick='jumpToParagraph()' it doesn't work. The problem with putting it inside any tag other than an href is that unless you use an href, the browser doesn't change the cursor to a hand, so many people will thing that the thing you're supposed to click on isn't clickable, even if it should be. Any ideas on how to make it work inside an href tag?
EmmyS
The reason <a href="#" onClick='jumpToParagraph()'> doesn't work is that by clicking, the browser uses the '#' in the href as its anchor link... and it leads to nowhere. You'll have to use javascript: in the href if you want it to work (see my edited version above).
Gert G
A: 

Hmm, you could try using document.body.scrollTop = document.getElementById(elementId).offsetTop; (not tested)

dhh
Nope, doesn't appear to do anything. The code to change visibility works, but nothing else happens. No errors. I've tested in Firefox 3.6.3, Chrome, and IE8.
EmmyS
A: 

Hum, the following JavaScript code works like a charm:

<script type="text/javascript">
function scrollToPos() {
    var el = document.getElementById("abc");
    el.style.visibility = "visible";
    el.style.display = "block";
    el.scrollIntoView(true);
}
</script>

When clicking this link <a href="#" onclick="scrollToPos();return false;">scroll</a><br /> the following div get's gets visible and scrolls into view (tested in IE6, IE8, FF3.6.3, Google Chrome 4.1 and Opera 10.5, all on windows)

<div id="abc" style="height:100px;color:red;font-weight:bold;visibility:hidden;display:none;">
    abc
</div>
dhh
That one doesn't work either. Obviously there's something strange going on, but I've copied your code directly (same as I did all the other answers) and even renamed my div to match yours just in case. It still just flashes briefly and unhides the form but does not jump to it. The strange thing is that Firebug isn't giving me any errors; it just isn't doing what you all say it should be doing.
EmmyS
A: 

OK, I finally found something that works. I've been doing what I was taught to do in the Stone Age: when using javascript calls in what needs to be a link, use

a href="#" onClick="yourFunction()"

Apparently it's the # that's killing things for me; if I just use

a href="javascript:yourFunction()"

it works correctly. This may or may not be considered good practice anymore, but it works.

EmmyS
Your solution wasn't the same until you edited it, after I posted my answer.
EmmyS