Jump to content

$(function(){}??


detestableguy

Recommended Posts

I have got a code about rating. My problem is that a link calls javascript in a following way

 

<a href='javascript:;'  class='vote_up' id='27'>Vote Up!</a>

 

where the javascript begins in following way

 

$(function(){

$("a.vote_up").click(function(){

//get the id

the_id = $(this).attr('id');

        ............................

        ........... goes on...........

}

 

My problem is I want to convert this link in something like "<a href="javascript(rating (id))" />

 

How can I do this? And please also tell me what does it mean when a function starts with "$" sign?

 

 

 

 

Link to comment
Share on other sites

My problem is I want to convert this link in something like "<a href="javascript(rating (id))" />

 

Why? The method you are using is much better as it allows you to keep your javascript separate from your markup.

 

And please also tell me what does it mean when a function starts with "$" sign?

 

Nothing in particular. $ is just another variable. In this case it looks like it is the jQuery framework.

Link to comment
Share on other sites

As far as it's use in this case and in general it seems the dollar sign ($) is used for abbreviating document.GetElementById. In this case:

 

$("a.vote_up").click(function()

 

Would be like writing:

 

document.GetElementById("a.vote_up").click(function()

 

I haven't had much luck using it, but from a bit of googling this is the best answer I came up with back when I was trying to figure out  the same thing.

 

Link to comment
Share on other sites

As far as it's use in this case and in general it seems the dollar sign ($) is used for abbreviating document.GetElementById. In this case:

 

Nope, in this case (as I said) it is the jQuery object.

Link to comment
Share on other sites

As far as it's use in this case and in general it seems the dollar sign ($) is used for abbreviating document.GetElementById. In this case:

 

Nope, in this case (as I said) it is the jQuery object.

 

Do explain to us humble muggles, if you have the time.

Link to comment
Share on other sites

jQuery is a popular Javascript framework (See http://jquery.com). It uses $ to refer to the jQuery object which in turn provides all of jQuery's functionality.

 

var foo = $('#foo');

 

The variable foo would now hold a reference to the element with an id of 'foo' wrapped within a jQuery object. This allows you to easily use jQueries methods on said element. eg;

 

var foo = $('#foo');
foo.click(function() {
  alert('You clicked the foo element');
});

 

Where 'click' is a method of the jQuery object.

Link to comment
Share on other sites

The problem is  I am calling multiple javascript files in a .php page. Thats why the script is not working.

 

If I do not import other javascripts, then it works fine.

 

Please help me, tell me how can I call that particular function to run "rating" script?

 

Attached file is that javascript file which is called by the following html code

 

<a href='javascript:;'  class='vote_up' id='27'>Vote Up!</a>

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.