Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add onclick property to input with JavaScript

Tags:

I am creating a user input at one of the events:

var throwConnectBox = function() {     chat_box = document.getElementById('box');     div = window.parent.document.createElement('div');     input = window.parent.document.createElement('input');     input.type = "submit";     input.value = "Join chat";     input.onclick = "conn.send('$connect\r\n');";     div.appendChild(input);     chat_box.appendChild(div); } 

... but the resulting input does not have onclick property. I tried to use

    input.onclick = conn.send('$connect\r\n'); 

... instead, but didn' work either. What am I doing wrong?

like image 705
Alex Avatar asked Apr 25 '09 22:04

Alex


People also ask

Can you put Onclick on input?

Description. The onclick property of an Input object specifies an event handler function that is invoked when the user clicks on the input element. It is not invoked when the click( ) method is called for the element. Only form elements that are buttons invoke the onclick event handler.

Which is the correct attribute to attach a click event to an element?

The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.


1 Answers

Try this:

 input.onclick = function() { conn.send('$connect\r\n'); }; 

Steve

like image 154
Steve Harrison Avatar answered Sep 27 '22 18:09

Steve Harrison