Link to home
Start Free TrialLog in
Avatar of thenelson
thenelson

asked on

help with jquery sendDigits

I have a textbox:
       <input type="text" id="tocall" value="">
and a 0-9, *, # telephone keypad on a webpage created by php.

The following code sends the digits when the keypad buttons are clicked with the mouse:
			$.each(['0','1','2','3','4','5','6','7','8','9','star','pound'], function(index, value) { 
		    	$('#button' + value).click(function(){ 
					if(connection) {
						if (value=='star')
							connection.sendDigits('*')
						else if (value=='pound')
							connection.sendDigits('#')
						else
							connection.sendDigits(value)
						return false;
					} 
					});
			});

Open in new window


I would like to add the function where I can paste numbers into the "tocall" textbox, press enter and have the digits sent consecutively by sendDigits when the call is connected:
                  Twilio.Device.connect(function (conn) {
                        $('#status').text("Successfully established call");
                        toggleCallStatus();

How can I do that?

Here is the rest of the jquery code:
		$(document).ready(function(){

			Twilio.Device.setup("<?php echo $token->generateToken(86400);?>");
			var connection=null;
			
			$("#call").click(function() {  
				params = { "tocall" : $('#tocall').val()};
				connection = Twilio.Device.connect(params);
			});
			$("#hangup").click(function() {  
				Twilio.Device.disconnectAll();
			});

			Twilio.Device.ready(function (device) {
				$('#status').text('Ready to start call');
			});

			Twilio.Device.incoming(function (conn) {
				if (confirm('Accept incoming call from ' + conn.parameters.From + '?')){
					connection=conn;
				    conn.accept();
				}
			});

			Twilio.Device.offline(function (device) {
				$('#status').text('Offline');
			});

			Twilio.Device.error(function (error) {
				$('#status').text(error);
			});

			Twilio.Device.connect(function (conn) {
				$('#status').text("Successfully established call");
				toggleCallStatus();
			});

			Twilio.Device.disconnect(function (conn) {
				$('#status').text("Call ended");
				toggleCallStatus();
			});
			
			function toggleCallStatus(){
				$('#call').toggle();
				$('#hangup').toggle();
				$('#dialpad').toggle();
			}

			$.each(['0','1','2','3','4','5','6','7','8','9','star','pound'], function(index, value) { 
		    	$('#button' + value).click(function(){ 
					if(connection) {
						if (value=='star')
							connection.sendDigits('*')
						else if (value=='pound')
							connection.sendDigits('#')
						else
							connection.sendDigits(value)
						return false;
					} 
					});
			});

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thenelson
thenelson

ASKER

It ended up easier than you suggested:
<input type="text" id="digits" /><input type="button" value="Send Digits" id="send"/>

$("#send").click(function() {
	if (connection) {
		connection.sendDigits($('#digits').val());
	}
});

Open in new window

but you helped me get started.

Thanks!!