1

I have a ID generated from jQuery using two different fields. Like I have a Fieldname, FieldCounter which is passed from backend. My Id is declared as attribute in jQuery as

htmlInputText.attr({
        'name': fieldNode.FieldName + fieldCounter,
         'id': fieldNode.FieldName + fieldCounter
     });

This Id changes based on the fieldname and field Counter. I need to target a Id which has FieldName as "EMP" and FieldCounter as "_BestPhone". The output Id is "EMP_BestPhone". When I try to target it by using JQuery as

$('#EMP_BestPhone').css('background', 'red');

It doesn't do anything. Anyone know how to do that.

I tried

$('#EMP_BestPhone').css('background', 'red');
$(htmlInputText).find('#EMP_BestPhone').css('background', 'red');

where htmlInputText contains the whole input tag with this Id like:

htmlInputText.attr({
        'name': fieldNode.FieldName + fieldCounter,
         'id': fieldNode.FieldName + fieldCounter
     });

$('#EMP_BestPhone').css('background', 'red');

$(htmlInputText).find('#EMP_BestPhone').css('background', 'red');

where htmlInputText contains the whole input tag with this Id like:

<input type="text" class="dforms_be_input_limits" name="EMP_BestPhone" id="EMP_BestPhone" maxlength="10" data-type="int" title="">

Target the Id

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Amarender Reddy
  • 243
  • 2
  • 14
  • 1
    No errors or warnings in console, This Id is used only once as the Field name and FieldCounter changes for the IF condition which is used above it. – Amarender Reddy Aug 27 '19 at 15:18
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Heretic Monkey Oct 15 '20 at 14:13

2 Answers2

0

The solution you have works perfectly fine. May be you are targeting it before it is loaded. Select the Id after it is loaded, it should work. I believe you are trying to target the Id before it is loaded. Use something like $(window).on('load', function() { // code here }); or document.ready.

-1

try changing the background color with

document.getElementById('#EMP_BestPhone').style.backgroundColor = "red";