SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run the function attached to click event

    Greetings, I have a SetListeners() function that assigns a click event
    to a radio button element. getAction() checks if the radio is checked,
    if so, I need to execute the click event. Is this possible? Thanks.
    Code:
    function SetListeners()
    {
    	var oradio = document.getElementById('rad1');
        if (oradio != null) {
    		addEvent(oradio, 'click', function() {alert('running now...');}, false);
    	}
    }
    //--------------------------------
    function getAction()
    {
    	var oradio = document.getElementById('rad1');
    	if (oradio.checked) {
    		// Run the code attached to the click event.
    	}
    }

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are attaching an anonymous function to to the radio button. Instead name it, and you will be able to call this function from whereever you want.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    peperjeria, do I need to give it a name and move it outside the the SetListeners() function?

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, correct. Try it out!

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did it. Thanks Peprjeria.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •