0

I have an array of elements (their ID's).

stage1check("htmlelement1", "htmlelement2", "htmlelement3");

These are input fields on a form, How can I change their background colour to red with jQuery? I have tried many times today but I always get an error.

 jQuery(stage1check).each(function() {
            console.log("else executed");
            // highlight stage 1 red (required empty fields)    
        });

Scratching my head... how do I like jQuery("#" + stage1check[i]).css("background-color:red;");for each one?

8
  • You might try the documentation for .each. Commented Oct 10, 2018 at 0:05
  • 2
    See css(). "This method can take either a property name and value as separate parameters, or a single object of key-value pairs." e.g. .css('background-color','red') or .css({'background-color':'red'}). Alternatively, use addClass() and define a CSS class with background-color:red;. Arguably, instead of using IDs, apply the same class to all elements and set background-color:red for that class. Commented Oct 10, 2018 at 0:05
  • You say stage1check is an array, but the first code looks to imply that it's a function...? Commented Oct 10, 2018 at 0:05
  • I just wrote the array out my actual array is working @CertainPerformance Commented Oct 10, 2018 at 0:08
  • You might want to clarify that then, eg change to const stage1check = ["htmlelement1", "htmlelement2", "htmlelement3"]; Commented Oct 10, 2018 at 0:10

3 Answers 3

1

Two issues were mentioned in comments:

  1. The syntax for the array is not valid.

  2. The syntax for css() is not valid.

Also, as mentioned by msg, iteration might not be necessary as you can select a jQuery collection of all the IDs and apply the css to that collection.

var stage1check = ["htmlelement1", "htmlelement2", "htmlelement3"];

jQuery('#' + stage1check.join(',#')).css({
  'background-color': 'red'
});
div {
  margin: 0 0 .5em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="htmlelement1">1</div>
<div id="htmlelement2">2</div>
<div id="htmlelement2b">2b</div>
<div id="htmlelement3">3</div>

Alternatively, use a class:

var stage1check = ["htmlelement1", "htmlelement2", "htmlelement3"];
jQuery('#' + stage1check.join(',#')).addClass('selected');
div {
  margin: 0 0 .5em;
}

.selected {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="htmlelement1">1</div>
<div id="htmlelement2">2</div>
<div id="htmlelement2b">2b</div>
<div id="htmlelement3">3</div>

Sign up to request clarification or add additional context in comments.

Comments

0

If it is a normal JS array, you can just use forEach():

yourArray.forEach((element) => {
  jQuery("#" + element).css('background-color','red');
});

3 Comments

Thanks for your help, what does the (element) represent?
Is it the number of the item in array?
@Scott It's the current iteration's value from the array. So, forEach passes each value from the array to its function, one-by-one, as the variable "element". See forEach(). Also, the () => {} format is called an arrow function.
0

You can use jQuery's .each and .css methods. Note that the parameters for each are in a different order to forEach.

.each isn't needed though if using .css:

/*
$('[id^="d"]').each((i, node) =>
  $(node).css('background-color','red')
);
*/

$('[id^="d"]').css('background-color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="d0">d0</div>
<div id="d1">d1</div>
<div id="d2">d2</div>

You can also do it with plain js:

document.querySelectorAll('[id^="d"]').forEach(node =>
  node.style.backgroundColor = 'red'
);
<div id="d0">d0</div>
<div id="d1">d1</div>
<div id="d2">d2</div>

1 Comment

You don't actually need the .each, .css works on collections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.