Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3229574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:49:35+00:00 2026-05-17T16:49:35+00:00

Need to turn javascript (ajax) vote script into reusable (dynamic) script so it can

  • 0

Need to turn javascript (ajax) vote script into reusable (dynamic) script so it can be used over and over.

I have some ajax that works great, but I have to reproduce it and add unique identifiers [where you see 9 in the code are the unique identifiers that I have to change] for every question added that needs a vote. Need to make it dynamic so it will reproduce itself or can be reused with onclick identifiers no matter how many questions are in the database.

Tried to put the javascript in-between <CFOUTPUT QUERY=”GetVotes”> </CFOUTPUT> tags so I could change the 9 to what ever the #GetVoteID# is for the vote question and dynamically reproduce the script as the questions are reproduce from the CFOUTPUT. But that didn’t work as the #GetVoteID# in the javascript caused the script not to work and I don’t know how to make the script reusable with onclick identifiers.

I see the thumbs up and down vote questions on lots of sites and the questions are dynamically reproduce though a database output, what are they doing to reuse there ajax?

I know onclick identifiers are the way to go, so any help on how I can convert my script, CFC’s, and whatever else so that it can handle 10, 20 or 1000 questions that need a vote.

Below is my code (The links, cfajaxproxy, CFC’s, and javascript)

Yes / No links
<CFOUTPUT QUERY="GetVoteList">
<A HREF="javascript:()" onclick="GetVoteYes9(#GetVoteID#);">Yes</A>
<A HREF="javascript:()" onclick="GetVoteNo9(#GetVoteID#);">No</A>
</CFOUTPUT>

Style for ajax
<STYLE>
GetVoteDescription9{visibility : visible}
</STYLE>

cfajaxproxy
<cfajaxproxy cfc="CFC/GetVoteYes9" jsclassname="YesVote9CFC">
<cfajaxproxy cfc="CFC/GetVoteNo9" jsclassname="NoVote9CFC">

YesVoteCFC

<cfcomponent>
    <cffunction name="NewCount9" access="remote">
        <CFQUERY NAME="YesCount9CK" DATASOURCE="MyDSN">
            SELECT  *
            FROM    GetVote
            WHERE   GetVoteID = <cfqueryparam value="9" cfsqltype="CF_SQL_INTEGER">
        </CFQUERY>
        <CFSET NewCount9=#YesCount9CK.YesCount#+1>
        <CFQUERY NAME="UpdateYesCount" DATASOURCE="MyDSN">
            UPDATE  GetVote
            SET     YesCount = <cfqueryparam value="#NewCount9#" cfsqltype="CF_SQL_INTEGER">
            WHERE   GetVoteID = <cfqueryparam value="9" cfsqltype="CF_SQL_INTEGER">
        </CFQUERY> 
        <cfreturn NewCount9>
    </cffunction>
</cfcomponent>

NoVoteCFC

<cfcomponent>
    <cffunction name="NewCount9" access="remote">  
        <CFQUERY NAME="NoCount9CK" DATASOURCE="MyDSN">  
            SELECT  *  
            FROM    GetVote   
            WHERE   GetVoteID = <cfqueryparam value="9" cfsqltype="CF_SQL_INTEGER">  
        </CFQUERY>  
        <CFSET NewCount9=#NoCount9CK.NoCount#+1>  
        <CFQUERY NAME="UpdateNoCount" DATASOURCE="MyDSN">  
            UPDATE  GetVote  
            SET     NoCount = <cfqueryparam value="#NewCount9#" cfsqltype="CF_SQL_INTEGER">  
            WHERE   GetVoteID = <cfqueryparam value="9" cfsqltype="CF_SQL_INTEGER">  
        </CFQUERY>   
        <cfreturn NewCount9>  
    </cffunction> 
</cfcomponent>

ajax script

<SCRIPT TYPE="text/javascript">
    function GetVoteNo9()  
    {   
        var GetVoteNo9 = document.getElementById("GetVoteNo9"); 
        var cfc = new NoCFC9();  
            cfc.setCallbackHandler(getDataResultNo9);  
            cfc.NewCount9(true);  
        var GetVoteDescription9 = document.getElementById("GetVoteDescription9").style.display='none'; 
        var content = document.getElementById('YesResponse9').innerHTML='';
        var content = document.getElementById('NoResponse9').innerHTML='You voted No';
    }  
    function getDataResultNo9(NoResult9)  
    {  
        var content = document.getElementById('NoCount9').innerHTML=NoResult;
    } 

    function GetVoteYes9()  
    {  
        var GetVoteYes9 = document.getElementById("GetVoteYes9"); 
        var cfc = new YesCFC9();  
            cfc.setCallbackHandler(getDataResultYes9);  
            cfc.NewCount9(true);  
        var GetVoteDescription9 = document.getElementById("GetVoteDescription9").style.display='none'; 
        var content = document.getElementById('YesResponse9').innerHTML='You voted Yes';
        var content = document.getElementById('NoResponse9').innerHTML='';
    }  
    function getDataResultYes9(YesResult9)  
    {  
        var content = document.getElementById('YesCount9').innerHTML=YesResult;
    }
</SCRIPT>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:49:36+00:00Added an answer on May 17, 2026 at 4:49 pm

    This is untested. Basically you need to generalize the code you’re writing. Everything below reflects this approach.

    Vote.cfc (replaces YesVoteCFC/NoVoteCFC)

    <cfcomponent>
        <cffunction name="NewCount" access="remote">
        <cfargument name="getVoteId">
        <cfargument name="vote">
        <cfset var choice = structNew()>
        <cfset choice.count = 0>
        <cfset choice.vote = arguments.vote>
        <cfargument name="vote">
        <CFQUERY NAME="VoteCount" DATASOURCE="MyDSN">
            SELECT  NoCount, YesCount 
            FROM    GetVote
            WHERE   GetVoteID = <cfqueryparam value="#arguments.voteId#" cfsqltype="CF_SQL_INTEGER">
        </CFQUERY>
        <cfif arguments.vote>
            <CFSET choice.count = VoteCount.YesCount + 1>
        <cfelse>
            <CFSET choice.count = VoteCount.NoCount + 1>
        </cfif>
    
        <CFQUERY NAME="UpdateYesCount" DATASOURCE="MyDSN">
            UPDATE  GetVote
            SET     <cfif arguments.vote>YesCount<cfelse>NoCount> = <cfqueryparam value="#choice.count#" cfsqltype="CF_SQL_INTEGER">
            WHERE   GetVoteID = <cfqueryparam value="#arguments.voteId#" cfsqltype="CF_SQL_INTEGER">
        </CFQUERY> 
        <cfreturn choice>
    </cffunction>
    

    cfajaxproxy

    <cfajaxproxy cfc="CFC/Vote" jsclassname="VoteCFC">
    

    Yes / No links

    <CFOUTPUT QUERY="GetVoteList">
    <A HREF="javascript:()" onclick="GetVote('#GetVoteID#', 'Yes');">Yes</A>
    <A HREF="javascript:()" onclick="GetVote('#GetVoteID#', 'No');">No</A>
    </CFOUTPUT>
    

    ajax script

    <SCRIPT TYPE="text/javascript">
        function GetVote(id, vote)  
        {   
            var GetVoteNo9 = document.getElementById("GetVote" + vote + id); 
            var cfc = new VoteCFC();  
                cfc.setCallbackHandler(getDataResult);  
                cfc.NewCount(id, vote);  
            document.getElementById("GetVoteDescription" + id).style.display='none'; 
            document.getElementById('YesResponse' + id).innerHTML= (vote == 'Yes' ? 'You voted Yes' : '');
            document.getElementById('NoResponse' + id).innerHTML= (vote == 'No' ? 'You voted No' : '');
        }  
        function getDataResultNo9(choice)  
        {  
            var content = document.getElementById(choice.vote + 'Count9').innerHTML=choice.count;
        } 
    
    </SCRIPT>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that contains a javascript function that i need to init/call
I have a map of several counties I need to turn into a county
need ask you about some help. I have web app running in Net 2.0.
I have been working on a project that dynamically creates a javascript file using
I need to know how to turn on Code Coverage when running TFS builds
Need a function that takes a character as a parameter and returns true if
Need to an expression that returns only things with an I followed by either
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
Quick Question. Eval in JavaScript is unsafe is it not? I have a JSON
I need to wait for an ajax response inside a for loop. If I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.