Simple jquery question, mostly a syntax thing I think

Associate
Joined
20 May 2007
Posts
441
Hey everyone

This is pretty simple thing and I know Im being a little bit uptight about how to go about this as I know one method that works, but I was hoping to do it my way :D but im new to jquery so for all I know it may not be possible.

Anyway this is the code I know that works

Code:
<script type="text/javascript">
    $(document).ready(
        function()
	{
	    $("#Prof").hide();
	}
    );
</script>

however I was kind of hoping to achieve this by doing the following

Code:
<script type="text/javascript">
    $(document).ready(
        initPage()
    );
	
    function initPage()
    {
        $("#Prof").hide();
    }
</script>

Is this possible or am I being stupid wanting to achieve this, this way?

Cheers
Matt
 
Fixed it lol

Spent quite awhile on this and then before you know it I mucked around and found the fix lol

Code:
<script type="text/javascript">
	$(document).ready(
		function()
		{
			initPage();
		}
		);
	
	function initPage()
	{
		$("#debbyFullProf").hide();
	}
	</script>
 
There's no need to wrap initPage in an anonymous function:

Code:
$(document).ready(initPage);

function initPage()
{
	$("#debbyFullProf").hide();
}

It's better practice to use the first way of doing it that you posted, though. Declaring a named function is redundant and pollutes the global namespace unnecessarily.
 
Hey everyone

This is pretty simple thing and I know Im being a little bit uptight about how to go about this as I know one method that works, but I was hoping to do it my way :D but im new to jquery so for all I know it may not be possible.

Anyway this is the code I know that works

Code:
<script type="text/javascript">
    $(document).ready(
        function()
    {
        $("#Prof").hide();
    }
    );
</script>
however I was kind of hoping to achieve this by doing the following

Code:
<script type="text/javascript">
    $(document).ready(
        initPage()
    );
    
    function initPage()
    {
        $("#Prof").hide();
    }
</script>
Is this possible or am I being stupid wanting to achieve this, this way?

Cheers
Matt

Surely your first way is best?
 
Huh? The first code snippet in your original post works and is the best one to use! Declaring a separate initPage function is unnecessary and could be considered bad practice.
 
In the first one you use "#Prof" but in the second "#debbyFullProf" maybe that was why? :p.
 
Back
Top Bottom