Reputation: 3786
I have
if ($('#logincap').length) // see if login cap exists or not
{
challengeField = $("input#recaptcha_challenge_field").val();
responseField = $("input#recaptcha_response_field").val();
}
else
{
challengeField = "nope";
responseField = "nope";
}
I want to make it read from a div called "logincap" only. So multiple recaptcha_challenge_field
and recaptcha_response_field
can be in the dom.
I know to do the validate and stuff in php. if php reads anything other then nope, it will try to validate the captcha with Google, else check if the account is "trusted". So i just isolated this part for you to look at. Do you know what i would have to change to make it read from "logincap" recaptcha_response_field
and recaptcha_challenge_field
?
Upvotes: 1
Views: 166
Reputation: 16858
This question isn't really worded especially well, but I'm going to take a stab at what I think you mean.
You can narrow the field of a jQuery selection by using context. Context is a section of your DOM that you can use as an optional argument in any further jQuery selections.
For example:
var context = $('#myElement')[0];
I can now query other DOM elements within the "myElement" node like so:
$('table', context).show();
This would ensure jQuery looks for table elements only within the nodes contained in my context variable ("myElement" in this case).
So to answer your question (hopefully), you would set a context like this:
var context = $('#logincap')[0];
You could then query your fields like this:
$('input#recaptcha_challenge_field', context').val();
Now: the obligatory warning. You shouldn't be using multiple IDs on your DOM anyway. Each ID attribute for your elements should be unique. Consider using the name attribute if you want to refer to the same value across multiple elements or consider using suffixes like #recaptcha_challenge_field-1
instead.
Upvotes: 1
Reputation: 26
I am making the assumption that you have the #logincap div wrapping around the #recaptcha_{challenge,response}_fields, so would not the easiest solution then be to target the correct fields using both #logincap and the recaptcha-fields like so:
if ($('#logincap').length) // see if login cap exists or not
{
challengeField = $("#logincap > input#recaptcha_challenge_field").val();
responseField = $("#logincap > input#recaptcha_response_field").val();
}
else
{
challengeField = "nope";
responseField = "nope";
}
Or am I missing something obvious?
On a completely different note: I don't think it would be valid html to serve up more than one element with the same id (i.e. multiple elements identified as recaptcha_{challenge,response}_field)
Anyway, hope it helps :)
Upvotes: 1