trickery.net  

Go Back   trickery.net > Technical > Programming

Reply
 
Thread Tools
Old 18-Aug-2015, 12:06   #1
BFH
Fixer of dead hard drives
BFH's Avatar
Join Date: Oct 2003
Location: The Final Frontier
Posts: 3,318
BFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond repute
Jquery selecting a specific form element thats repeated

I've got a form that is repeated multiple times on the page.

Within that form are two fields, for organisation name and corresponding ID.

This code:

Code:
jQuery(document).ready(function(){
		$('#orgname').autocomplete({
		source:'includes/searchclientname.php', 
		minLength:1,
		select:function(evt, ui)
		{
			document.getElementById("orgid").value = ui.item.orgid;
		}
	});

	
});
Basically matches up the name as it's entered and spits out the id into the hidden ID field.

The problem is that it's only working for the 1st form on the page - all the other orgname boxes in the other forms are ignored, so I need it to pickup the selected orgname field each time rather than always assuming its the first one on the page...

If you follow.

I've tried the "this" selector but not sure I'm doing it right - any pointers?
__________________
Elite Dangerous: Cmdr Naughtius Maximus
Moto: fuck it
BFH is offline  
Reply With Quote
Old 18-Aug-2015, 19:19   #2
The Dark One
meh
The Dark One's Avatar
Join Date: Oct 2003
Posts: 8,091
The Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond reputeThe Dark One has a reputation beyond repute
You could loop over all the matching elements and add the autocomplete handler to each?

Code:
$( document ).ready(function() {
 
	$( "#orgname" ).each(function() {		
		$( this ).autocomplete({
			source:'includes/searchclientname.php', 
			minLength:1,
			select:function(evt, ui) {
				document.getElementById("orgid").value = ui.item.orgid;
			}
	});
 
});
$( document ) is more habit, I've never really used jQuery(document).ready(function(){ as a syntax, both are perfectly valid

Last edited by The Dark One; 18-Aug-2015 at 19:26.
The Dark One is offline  
Reply With Quote
Old 18-Aug-2015, 19:44   #3
BFH
Fixer of dead hard drives
BFH's Avatar
Join Date: Oct 2003
Location: The Final Frontier
Posts: 3,318
BFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond repute
Hmm ok - ta - will give that a bash tomorrow and see what happens.

Cheers
__________________
Elite Dangerous: Cmdr Naughtius Maximus
Moto: fuck it
BFH is offline  
Reply With Quote
Old 19-Aug-2015, 10:35   #4
BFH
Fixer of dead hard drives
BFH's Avatar
Join Date: Oct 2003
Location: The Final Frontier
Posts: 3,318
BFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond repute
Hmm browser said your code was missing stuff " )} " - i've added this in so the error has cleared up but I can still only use the first org name box on the page..

Code:
$( document ).ready(function() {
	$( "#orgname" ).each(function() {		
		$( this ).autocomplete({
			source:'includes/searchclientname.php', 
			minLength:1,
			select:function(evt, ui) {
				document.getElementById("orgid").value = ui.item.orgid;
			}
			})
	});
	
	
});
__________________
Elite Dangerous: Cmdr Naughtius Maximus
Moto: fuck it
BFH is offline  
Reply With Quote
Old 19-Aug-2015, 10:54   #5
Ned
posts for a living
Join Date: Dec 2003
Location: Brighton
Tournaments Won: 1
Posts: 4,350
Ned has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond repute
Multiple elements with the same ID (orgid), therefore it selects the first one?
__________________
Geckos | Reptile Vets
Ned is offline  
 neddage 
Reply With Quote
Old 19-Aug-2015, 10:58   #6
Brocken
Bit lost
Brocken's Avatar
Join Date: Oct 2003
Posts: 5,850
Brocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond repute
Firstly make sure you're not using the same ID more than once. You can't do $('#orgname').each(...) because there should only be a single element with the ID "orgname".

Code:
$('form.whatever').each(function() {
	var $this = $(this);
	$this.autocomplete({
		source:'includes/searchclientname.php', 
		minLength:1,
		select:function(evt, ui) {
			$this.children('.hidden-field-class').first().val(ui.item.orgid);
		}
	});
});
Use .find() instead of .children() if the hidden field isn't a top level element inside the form.

There's almost certainly a better way of doing that but it should work if you put in the right selectors.
Brocken is offline  
Reply With Quote
Old 19-Aug-2015, 12:55   #7
BFH
Fixer of dead hard drives
BFH's Avatar
Join Date: Oct 2003
Location: The Final Frontier
Posts: 3,318
BFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond repute
The field isn't hidden though - its for the user to enter a client name into which the jquery stuff then provides as a drop down list from the existing data - ie. matching as they type.

My problem is that I could have any number of these client fields visible at once so need a way to have this code run against the one that is active - and as you say, there can only be one element with that ID as a name so it makes send the code should only work on the first one found. Presumably then I should give them a NAME rather than ID?
__________________
Elite Dangerous: Cmdr Naughtius Maximus
Moto: fuck it

Last edited by BFH; 19-Aug-2015 at 12:59.
BFH is offline  
Reply With Quote
Old 19-Aug-2015, 13:24   #8
Ned
posts for a living
Join Date: Dec 2003
Location: Brighton
Tournaments Won: 1
Posts: 4,350
Ned has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond reputeNed has a reputation beyond repute
Yeah use name rather than ID. Or give them a unique ID which you can somehow tie with the input the auto complete is on.

Better might be to do something like this:

https://jsfiddle.net/dat3df7m/

I've used focusout just as an example trigger as not got the autocomplete stuff on jsfiddle.

It gets the parent div and then find the hidden input (based on class name) within it to update. So assuming each of your sections is within its own div/other wrapper then it should work.
__________________
Geckos | Reptile Vets
Ned is offline  
 neddage 
Reply With Quote
Old 19-Aug-2015, 16:36   #9
Brocken
Bit lost
Brocken's Avatar
Join Date: Oct 2003
Posts: 5,850
Brocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond reputeBrocken has a reputation beyond repute
Quote:
Originally Posted by Deceased View Post
The field isn't hidden though - its for the user to enter a client name into which the jquery stuff then provides as a drop down list from the existing data - ie. matching as they type.

My problem is that I could have any number of these client fields visible at once so need a way to have this code run against the one that is active - and as you say, there can only be one element with that ID as a name so it makes send the code should only work on the first one found. Presumably then I should give them a NAME rather than ID?
I meant the hidden ID field you mentioned in the first post.
Brocken is offline  
Reply With Quote
Old 19-Aug-2015, 17:06   #10
BFH
Fixer of dead hard drives
BFH's Avatar
Join Date: Oct 2003
Location: The Final Frontier
Posts: 3,318
BFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond reputeBFH has a reputation beyond repute
Quote:
Originally Posted by Brocken View Post
I meant the hidden ID field you mentioned in the first post.
yes of course - woken up now.
__________________
Elite Dangerous: Cmdr Naughtius Maximus
Moto: fuck it
BFH is offline  
Reply With Quote
Reply

Go Back   trickery.net > Technical > Programming

Tags
open it in notepad

Similar Threads
Thread Thread Starter Forum Replies Last Post
3.0.2 Patch Notes Minty World of Warcraft 90 27-Oct-2008 10:08

Users Viewing Thread: 1 (0 members and 1 guests)
 

Thread Tools

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 On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 14:06.


Powered by vBulletin® Version 3.7.0 Release Candidate 3
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright 2003 - 2013, trickery.net