LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2013, 09:20 PM   #1
slackaddict
Member
 
Registered: Nov 2004
Location: Aotearoa
Distribution: Slack, Ubuntu
Posts: 92

Rep: Reputation: 15
Question JavaScript: Function that detects click and returns control to calling function


Hi

I want to call a function that displays a list then returns control, along with the id of the clicked item, to the calling function.

I want it to work like this:
Code:
function createOrder() {
	customerId = getCustomerId();
	// Continue creating order now...
}

function getCustomerId() {
	// Display list of customers...
	// Return the id of clicked item
	return customerId;
}
But I don't know how to detect the click and return control to createOrder().

At the moment I do this:
Code:
function createOrder(customerId ) {
	if (typeof customerId == 'undefined') {
		customerId = getCustomerId(createOrder);
	} else {
		// Continue creating order now...
	}
}

function getCustomerId(callback) {
	var list = document.createElement('ul');
	// Sample data...
	for (i=0; i<9; i++) {
		var id = 'Cust' + i;
		var item = document.createElement('li');
		item.appendChild(document.createTextNode('Customer ' + id));
		item.id = id;
		item.addEventListener('click', function() { callback(this.id) });
		list.appendChild(item);
	}
	document.getElementById('test').appendChild(list);
}
But this isn't really returning control to the calling function, it just calls that function again, this time with a parameter.

How can I get javascript to detect the click and actually return to the calling function?

Thanks
 
Old 11-27-2013, 02:07 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Not possible. The script that is activated by the selection (onclick) should find out (from variables) that a creation is in progress.
 
Old 11-27-2013, 02:53 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Not sure why you would have that particular code structure, but it seems to me that you are doing it backwards calling createOrder() then expecting it to synchronously get the customerId. Think asynchronously...

Code:
function listCustomers() {
	var list = document.createElement('ul');
	// Sample data...
	for (i=0; i<9; i++) {
		var id = 'Cust' + i;
		var item = document.createElement('li');
		item.appendChild(document.createTextNode('Customer ' + id));
		item.id = id;
		item.addEventListener('click', function() { createOrder(this.id) });
		list.appendChild(item);
	}
	document.getElementById('test').appendChild(list);
}

function createOrder(customerId ) {

		// Continue creating order now...

}
 
  


Reply

Tags
javascript



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a function in 1 sec without using any timer based function/API barunparichha Linux - Software 3 09-09-2011 02:15 PM
calling external javascript function in xslt doesn't work in IE mohtasham1983 Programming 6 05-15-2009 02:25 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
Calling a function in javascript mohtasham1983 Programming 4 03-04-2008 04:13 PM
Calling a PHP function from another file returns a weird value mohtasham1983 Programming 1 07-27-2007 01:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration