PDA

View Full Version : How to execute double onclick event at one click



hackarena
04-23-2012, 06:20 PM
How do i use 2 onclick event at once.

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



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;
}
}




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.');}}

ApacheTech
04-23-2012, 06:36 PM
This is JavaScript, not PHP.

You do it with:


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.


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

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

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

hackarena
04-23-2012, 07:06 PM
This is JavaScript, not PHP.

You do it with:


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.


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

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

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

did you get my private message?

ApacheTech
04-23-2012, 07:28 PM
In reply to PM:


var t = 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(arg1, arg2), '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".