Results 1 to 4 of 4

Thread: How to execute double onclick event at one click

  1. #1
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to execute double onclick event at one click

    How do i use 2 onclick event at once.

    e.g, these two functions should be executed once a checkbox is checked.

    Code:
     function insert(elem) {
     var text1 = document.getElementById('text1');
     if(text1.value == "") {
     text1.value = elem.value;
     elem.disabled = "true";
     } else {
     text1.value = text1.value + " " + elem.value;
     }
     }

    Code:
    function boxchk(obj,max) {var box = obj.name.substr(0,obj.name.lastIndexOf('_')+1);var cnt=0,i=1;while(obj.form[box+i]) {cnt += obj.form[box+i].checked;i++;}if (cnt > max) {obj.checked = false;alert('Only choose ' + max + ' '+' Numbers.\nTo pick this number, unselect one of the others.');}}

  2. #2
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    This is JavaScript, not PHP.

    You do it with:

    PHP Code:
    doSomething() {
        
    // Do something here.
    }

    doSomethingElse() {
        
    // Do something else here.
    }

    element.addEventListener('click'doSomething'false');
    element.addEventListener('click'doSomethingElse'false'); 
    The 'false' argument of the addEventListener method can be changed to 'true' is you wish the event to bubble, but it's usually not needed.

    With this method, don't add any onclick event to the element inline.

    Edit:

    Two other ways you could do it is either to merge the functions into a single function, or call doSomethingElse from within doSomething.

    PHP Code:
    doSomething() {
        
    // Do something here.
        
    doSomethingElse();
    }

    doSomethingElse() {
        
    // Do something else here.
    }

    element.addEventListener('click'doSomething'false'); 

  3. #3
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ApacheTech View Post
    This is JavaScript, not PHP.

    You do it with:

    PHP Code:
    doSomething() {
        
    // Do something here.
    }

    doSomethingElse() {
        
    // Do something else here.
    }

    element.addEventListener('click'doSomething'false');
    element.addEventListener('click'doSomethingElse'false'); 
    The 'false' argument of the addEventListener method can be changed to 'true' is you wish the event to bubble, but it's usually not needed.

    With this method, don't add any onclick event to the element inline.

    Edit:

    Two other ways you could do it is either to merge the functions into a single function, or call doSomethingElse from within doSomething.

    PHP Code:
    doSomething() {
        
    // Do something here.
        
    doSomethingElse();
    }

    doSomethingElse() {
        
    // Do something else here.
    }

    element.addEventListener('click'doSomething'false'); 
    did you get my private message?

  4. #4
    Join Date
    Apr 2012
    Location
    Chester, Cheshire
    Posts
    329
    Thanks
    7
    Thanked 35 Times in 35 Posts

    Default

    In reply to PM:

    PHP Code:
    var document.getElementById('txtFirstValue');

    function 
    insertValue(e) {
        if (
    t.value == '') {
            
    t.value e.value;
            
    e.disabled "true";
        } else {
            
    t.value +=  ' ' e.value;
        }
    }

    function 
    validateValue(obj,max) {
        var 
    box obj.name.substr(0,obj.name.lastIndexOf('_')+1);
        var 
    cnt=0,i=1;

        while(
    obj.form[box+i]) {
            
    cnt += obj.form[box+i].checked;
            
    i++;
        }

        if (
    cnt max) {
            
    obj.checked false;
            
    alert('Only choose ' max ' Numbers.\n\nTo pick this number, deselect one of the others.');}
    }

    t.addEventListener('click'insertValue(arg1), 'false');
    t.addEventListener('click'insertValue(arg1arg2), 'false'); 
    Obviously, you'll have to change the arg1 and arg2 to your own passable values and I've taken the liberty to streamline and clean your code a bit. Be careful how you name things... Text1 means nothing to anyone who doesn't have the code in front of them and can cause even your own code to become unmanageable in larger applications. Use single quotes for strings, so that you can save the double quotes for literal quotes within the string. One last thing, the Grammar Nazi in me had to change "unselect" to "deselect".

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
  •