www.webdeveloper.com
Results 1 to 2 of 2

Thread: Exchange two 'onclicks' in IE

  1. #1
    Join Date
    Aug 2012
    Posts
    1

    Exchange two 'onclicks' in IE

    Hey,

    i want to exchange the onclick-events of two elements. The following code does want it should in Firefox but in IE it fails:

    Code:
    var tmpButton = button1.getAttribute('onclick');
    button1.setAttribute('onclick', button2.getAttribute('onclick'));
    button2.setAttribute('onclick', tmpButton);
    I tried the following to solve the Problem:

    Code:
    var tmpButton = button1.onclick;
    button1.onclick = button2.onclick;
    button2.onclick = tmpButton;
    after executing this code both elements lost their 'onclick'-attributes.
    Could you please help me?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    See if this works for you...
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    function btn1actions() { alert('Button acts as Button #1'); }
    function btn2actions() { alert('Button acts as Button #2'); }
    
    function swapActions(checkedStatus) {
      if (checkedStatus == true) {
        document.getElementById('btn1').setAttribute('onclick','btn2actions()');
        document.getElementById('btn2').setAttribute('onclick','btn1actions()');
      } else {
        document.getElementById('btn1').setAttribute('onclick','btn1actions()');
        document.getElementById('btn2').setAttribute('onclick','btn2actions()');
      }
    }
    </script>
    
    </head>
    <body>
    <button id="btn1" onclick="btn1actions()"> Button 1 </button>
    <button id="btn2" onclick="btn2actions()"> Button 2 </button>
    <p>
    <input type="checkbox" onchange="swapActions(this.checked)"> Swap actions
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles