skip to Main Content
hourArray = []


var hour9 = $("#9").attr("id") var hour10 = $("#10").attr("id") var hour11 = $("#11").attr("id") var hour12 = $("#12").attr("id") var hour13 = $("#13").attr("id") var hour14 = $("#14").attr("id") var hour15 = $("#15").attr("id") var hour16 = $("#16").attr("id") var hour17 = $("#17").attr("id")

var hour9N = parseInt(hour9) var hour10N = parseInt(hour10) var hour11N = parseInt(hour11) var hour12N = parseInt(hour12) var hour13N = parseInt(hour13) var hour14N = parseInt(hour14) var hour15N = parseInt(hour15) var hour16N = parseInt(hour16) var hour17N = parseInt(hour17)

hourArray.push(hour9N) hourArray.push(hour10N) hourArray.push(hour11N) hourArray.push(hour12N) hourArray.push(hour13N) hourArray.push(hour14N) hourArray.push(hour15N) hourArray.push(hour16N) hourArray.push(hour17N)


for (i = 0; i < hourArray.length; i++) {

if (hourArray[i] === hour) {

var color = $(hourArray[i]).toString();

    $(color).removeClass("row time-block")


else if (hourArray[i] < hour) {

var color = $(hourArray[i]).toString()



else {

var color = $(hourArray[i]).toString()

   }

I am trying to convert my integers back into string data so that I may target the appropriate members of the array. It works fine when I console log the data (console logs the appropriate message for the proper number of members of said array for each condition). But when I try to put the variable in the add/remove class method, it throws up an error saying unrecognized expression [object OBJECT]. Could someone please point me in the right direction?

Ive attempted to convert the integers back to strings but it does not seem to work for the add/remove class method.

2

Answers


  1. With this you are taking an integer and passing it to the jQuery constructor.
    I assume you meant to concat it with a ‘#’ in order to get a CSS selector for the id (see http://api.jquery.com/id-selector/). I don’t know what you’re trying to achieve with toString. You are converting the jQuery element which is pointing at the DOM element and throwing it away and replacing it with a String. A string is fine for logging, but no good for calling jQuery methods on.

    var color = $(hourArray[i]).toString();
    

    Probably want:

    var color = $('#' + hourArray[i]);
    color.removeClass("row time-block")
    
    Login or Signup to reply.
  2. you could also simplify this if you wanted by using a class on all your objects and going through them with a each loop. Also keep in mind that an ID that is just a number isn’t considered valid if you’re trying to meet html standards. if you want to store info on a element a custom data attribute might be your ideal solution.

    https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

    const hourArray = $('.hour');
    
    hourArray.each(function (index) {
        var offset = index + 9; //this is only needed because you start at nine
        if (offset === hour) {
            $(this).removeClass("row time-block");
        } else if (offset < hour) {
            //offset will be your color
        } else {
            //else stuff 
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search