Search code examples
javascriptscriptingserver-side-scripting

Dynamic Client Script


I need to write a java script. This is supposed to validate if the checkbox is selected in the page or not. The problem here is that the check box is inside a grid and is generated dynamically. The reason being the number of check box that need to be rendered is not know at design time. So the id is know only at the server side.


Solution

  • Here is a thought:

    As indicated by Anonymous you can generate javascript, if you are in ASP.NET you have some help with the RegisterClientScriptBlock() method. MSDN on Injecting Client Side Script

    Also you could write, or generate, a javascript function that takes in a checkbox as a parameter and add an onClick attribute to your checkbox definition that calls your function and passes itself as the parameter

    function TrackMyCheckbox(ck)
    {
         //keep track of state
    }
    
    <input type="checkbox" onClick="TrackMyCheckbox(this);".... />