Onclick doesn't yield expected results.

Hello people, I have looked at this code in and out and can’t seem to find out what the problem is. The topic says it all. I have a link on a webpage with id name comment_stream on which when users click, is supposed to display an otherwise hidden comment box (id name comment_stream_div) and at the same time, hide the comment link, you know the type on facebook and other social networking sites. I am using javascript functions to accomplish this with an onclick event. The idea is to toggle between the display attributes (block and none) of the elements i wish to hide or display with the onclick event. I will show the javasscript below, as well as the the html for the elements involved and the css.


<script  type='text/javascript'>

		function show_comment_stream_div() {

				document.getElementById('comment_stream_div').display = 'block';

				document.getElementById('comment_stream').display = 'none';
		}


</script>






  //Print the comment link

<p id = 'comment_stream' onclick = 'show_comment_stream_div()' > <a href = ''> Comments: </a> </p>";

//Print out the comments div.

<div id = 'comment_stream_div' onmouseover = 'show_close_comment_stream_div()' onmouseout = '                                                          hide_close_comment_stream_div()'> <p id = 'close_comment_stream_div' onclick = 'hide_comment_stream_div()'> <a> X </a> </p>

        <form action = 'status_comment_entry.php'>

              <input id = 'comment_stream_input' type = 'textarea' rows = '8' cols = '20' value ='' name = 'comment' /> <br/>

              <input id = 'comment_stream_submit' type = 'submit' value = 'submit!' name = 'submit' />

         </form>

</div>




#comment_stream
{position:relative;bottom:70px;left:230px; color:#2d73b9; display:block;}


#comment_stream_div
{position:relative;bottom:82px;left:123px; width:240px; height:75px; border-style:solid; border-width:1px; border-color:#c0c0c0; display:none;}

Here are screen shots of what i expect before and after clicking

Well folks, can anyone tell me what I’m doing wrong?

Hi dray,

You can do it with jQuery. Very simple; here’s the code I generated for you:

[php]

jQuery

Comments:

<p id = 'close_comment_stream_div'X
[/php]

@Codeguru, thanks for that, I’m going to try it out. Only problem is I don’t understand Jquery yet. Isn’t there a way around this with just simple JS? I have a few other situations that I need to use events to alter.

I got a suggestion from another forum, to access the display and visibility attributes as follow:
document.getElementById(‘id’).style.display = ‘value’
That seems to solve the problem but one more problem arises. The expected results just flash on the screen for a brief second or two, then the page resorts to its original settings. Any suggestions?

Sponsor our Newsletter | Privacy Policy | Terms of Service