Turning jquery into an includable callable function

0 votes

I'm so frustrated! As an ok PHP developer I can't get my head around the simplist of jquery problems!

I have recently moved my HTML jquery include to the end of the HTML body, instead of in the head to improve google pagespeed score.

This has broken some jquery which is used for simple comment voting. This was written badly as it repeats for every comment.

<div id="voterow-19907" class="commentfooter">UP</a> | <a id="comment-vote-down-19907" href="#" rel="nofollow">DOWN</a></div>


    <script>
    $("#comment-vote-up-19907").click(function() {
        $.ajax({
            type: "GET",
            url: "/ajax.php",
            data: "a=rv&v=19907&d=up",
            success: function(data){
                $("#voterow-19907").text("Thank you for your vote")
            }
        });
        return false;                 
    });

    $("#comment-vote-down-19907").click(function() {
        $.ajax({
            type: "GET",
            url: "/ajax.php",
            data: "a=rv&v=19907&d=down",
            success: function(data){
                $("#voterow-19907").text("Thank you for your vote")
            }
        });
        return false;                 
    });
    </script>

Since moving the jquery include to the bottom of the page this naturally doesn't work.

What I'm trying to do is turn the above code into a mini function I can include after the jquery include, then pass the ID and VOTE-DIRECTION to the function from the HTML a hrefs using the jquery DATA- attribute.

Any help would be greatly appreciated. I'm running out of hair!

Aug 8, 2022 in Web Development by gaurav
• 23,260 points
363 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Web Development

0 votes
1 answer

Is there an "exists" function for jQuery?

Yes! The $. fn. exists example is replacing ...READ MORE

answered Aug 4, 2022 in Web Development by rajatha
• 7,680 points
479 views
0 votes
1 answer

jQuery $(this) selector function and limitations

this isn't a jQuery "thing", but a basic JavaScript ...READ MORE

answered Jun 23, 2022 in Web Development by rajatha
• 7,680 points
678 views
0 votes
1 answer

how to use substr() function in jquery?

To get substring of a string in ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,274 views
0 votes
1 answer

jQuery Load function not loading the file

The jQuery load() method allows HTML or ...READ MORE

answered Jun 29, 2022 in Web Development by rajatha
• 7,680 points
3,841 views
0 votes
1 answer

How to change an element's title attribute using jQuery

You can change the title attribute with ...READ MORE

answered Jun 30, 2022 in Web Development by rajatha
• 7,680 points
4,699 views
0 votes
0 answers

jQuery ajax error function

I have an ajax call passing data ...READ MORE

Jun 29, 2022 in Web Development by gaurav
• 23,260 points
1,837 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,339 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
15,782 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,346 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,331 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP