1

When i construct my input manually i can focus on it however when i use a JSF tag to generate my inputs the form name is appended to the id and i can't reference it anymore.

I've tried

$("#username").focus();

and

$("#loginForm:username").focus();

without any luck. Any advise?

here is generated jsf html:

<input id="loginForm:username" type="text" name="loginForm:username" size="30" />

This is my how i'm triggering the call...

$(document).ready(function() {
    alert("checkA");
    $("#username").focus();
    alert("checkB");
});
simgineer
  • 1,754
  • 2
  • 22
  • 49

2 Answers2

3

The : is a special character in CSS identifiers. You need to escape it by \. Note that the \ is by itself also a special character in JavaScript strings, so to represent the actual \, you need to double-escape it as \\.

So, this should do:

$("#loginForm\\:username").focus();

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • nice, works like a charm! If i ever win the lottery i'll make sure a decent portion of it goes to the BalusC trust fund :) – simgineer Jul 20 '12 at 05:32
1

try:

$("input[name*='loginForm']").focus(); 
proggrock
  • 3,239
  • 6
  • 36
  • 51