Link to home
Start Free TrialLog in
Avatar of Rouchie
RouchieFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help using :first

I have multiple tables, each containing multiple text fields and links.  When a particular link is clicked, I need to find the first text field that matches a given ID value.
I have this attempt, but it doesn't find the text field.  Any help would be appreciated:

function ReplaceFirstValue(idToClear, startCtrl) {
		var startCtrlId = '#' + startCtrl;
		var firstTextField = $(startCtrlId).parent('table').find("input[type='text'][id$='" + idToClear  + "']:first"); // fails (no error given)
		$(firstTextField).val('Hello');
}

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

please check if by removing first, you get any results or not.

then rather than using :first try using first()
http://api.jquery.com/first/
 
Avatar of Rouchie

ASKER

What do you mean by removing?
i meant that if you replace line 3 with

var firstTextField = $(startCtrlId).parent('table').find("input[type='text'][id$='" + idToClear  + "']").size(); // fails (no error given)

console.log(firstTextField );

what is the output? if the output is 0, then it means that without using :first also there was no result
Avatar of Rouchie

ASKER

I must be doing something so stupid here but I cannot see for looking...

I've simplified the function just to start with the ID of the link that is pressed, but the function even fails to find that.  Can you see what's wrong here?

function ReplaceFirstValue(idToClear, startCtrl){
		window.alert('executing...');  // works okay
		var startCtrlId = '#' + startCtrl; // presumably does not work
		$(startCtrlId).hide(200);  // does not work
}

Open in new window


<a onclick="ReplaceFirstValue('txt_skillLevel', this); return false;" href="#">Replace</a>

Open in new window

>> I need to find the first text field that matches a given ID value.

My first red light here is that the DOM does not allow multiple ID's with the same value, which would also explain why your code is unable to find anything.
Avatar of Rouchie

ASKER

Hi

I appreciate that thanks.  

The ID's aren't actually duplicated, but end with the same value.  That's why I was using the id$= selector in the original post.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe 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 Rouchie

ASKER

Hi leakim971

That's works, thank you.  Right about the time you posted that I also found a solution using the following approach.  It turns out that parent should have been parents, but closest is better still.

$(startCtrl).closest('table').find(""input[type='text'][id$='"" + idToClear  + ""']"").first().val('Hello');

Open in new window


In your solution, could you please explain what effect table:eq(0) has?  I believe not including eq(0) works just as well also.
Avatar of Rouchie

ASKER

Sorry just copied out of ASP... mine should really read:

$(startCtrl).closest('table').find("input[type='text'][id$='" + idToClear  + "']").first().val('Hello');

Open in new window

ok, don't forget to close the question
Avatar of Rouchie

ASKER

In your solution, could you please explain what effect table:eq(0) has?  I think that not including eq(0) gives the same result.
No it may return more than one depend your page
Same as closest here