Skip to content Skip to sidebar Skip to footer

Finding The Login Form Based On Content

Basically I want to find out the login form when there are multiple forms on a HTML document. My guess is as following: What if i can identify to which form strings like 'forgot yo

Solution 1:

var loginFormId = $('form').filter(function() {
    returnthis.innerHTML.indexOf('forgot your password') != -1;
}).attr('id');​

Or simply with :contains:

var loginFormId = $('form:contains("forgot your password")').attr('id');​

:contains selector docs:

Description: Select all elements that contain the specified text. The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof. As with attribute value selectors, text inside the parentheses of :contains() can be written as a bare word or surrounded by quotation marks. The text must have matching case to be selected.

LIVE DEMO


Update:

If you are not sure about the case of the text, you must use filter:

var theFormId = $('form').filter(function() {
    var text = this.innerHTML.toLowerCase(); 
    return text.indexOf('forgot your password') != -1 || 
           text.indexOf('remember me') != -1;

Solution 2:

var loginForm;
$("form").each(function(){
    if($(this).text().indexOf("Remember me")>=0)
         loginForm = $(this);
})

Post a Comment for "Finding The Login Form Based On Content"