javascript get a input name?


Hello people,

I tried do define a background color to a input with a name "field1" but doesnt work.
> Example: $('["field1"]').css("background","red");

Or i try to obtain the value of a input field:
var a=document.getElementsByTagName("field1");

In the two cases it doesnt work, what seems to be the problem?

Hi Nuno,

The name of a widget doesn't map directly to its ID or name attribute in the generated HTML. The main reason for this is that a single widget with a name can appear multiple times in the rendered page (it can be inside a List widget or inside a webblock that is present multiple times). Since the both name and ID must be unique, the multiple instances of the widget wouldn't be able to all take their name (which would be the same for all of them) and use it as their ID.

If you need to refer to a widget through javascript you can annotate it by creating an extended property (eg: name: data-field, value: 1) and query it through the javascript (eg: $('input[data-field="1"]')).