Skip to main content
Known Participant
April 5, 2012
Question

Set Variable onClick

  • April 5, 2012
  • 3 replies
  • 2726 views

I want to set a varaible on a button click.  I have been trying this:

<a href="##" onclick="document.getElementById('faq1').style.display='block';    

                                 document.getElementById('faq2').style.display='none';    

                                 document.getElementById('faq3').style.display='none';

                                <cfset pageCount = pageCount + 1>">

<span class="contentHeader">Overview</span>

</a>

This does not seem to work.  Does anyone have any ideas how to make this work?  Thanks

    This topic has been closed for replies.

    3 replies

    BKBK
    Community Expert
    Community Expert
    April 7, 2012

    TheScarecrow wrote:

    I want to set a varaible on a button click.  I have been trying this:

    <a href="##" onclick="document.getElementById('faq1').style.display='block';    

                                     document.getElementById('faq2').style.display='none';    

                                     document.getElementById('faq3').style.display='none';

                                    <cfset pageCount = pageCount + 1>">

    <span class="contentHeader">Overview</span>

    </a>

    This does not seem to work.  Does anyone have any ideas how to make this work?  Thanks

    I see no button. I see no variable that needs setting either. Could you explain a bit more?

    Sean Coyne
    Participating Frequently
    April 6, 2012

    You are trying to set a ColdFusion (server side) variable using Javascript (client side).  You cannot run ColdFusion code in the browser.  If you look at the source, you will see that your <cfset ... /> is not passed along to the browser.

    You could call an ajax request which would execute CF code on the server to increase the count and store it in the user's session, for example, but I think you need to go back to the basics and understand the difference between client side code and server side.

    Inspiring
    April 6, 2012

    It's not clear what you are attempting.  The only variable I see is pageCount but I don't see anywhere where you are attempting to display it.

    Known Participant
    April 6, 2012

    I am not trying to display it.  I am keeping track of it and once it reaches three a button appears on the screen.