0

jquery is loaded in my html prior to my js code. Then in my puzzle.js I have :

class Puzzle {
  constructor(ID, Name, Status) {
    this.ID = "main" + ID;
    this.Name = Name;
    this.Status = Status;
    this.mainDiv = "";
  }

  generateDiv() {
    this.mainDiv = '<div id="' + this.ID + '"/>';
    $(this.mainDiv).addClass("puzzle");
  }
}

$(document).ready(function() {
  var puzzle = new Puzzle(1, "MyPuzzle", 1);
  puzzle.generateDiv();
  $("#Puzzles").append(puzzle.mainDiv);
})
.puzzle {
  width: 200px;
  height: 200px;
  background: black;
  border 1px solid white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Puzzles"></div>

And the div get's added with the nice ID field but the class does not. In fact anything I do to the div "jquery-style" does nothing (but also no console error)

I noticed that in the inspector (chrome) the div appears as

<div id="main1"></div> == $0

Not sure what it means or if relevant.

What am I doing wrong ???

5
  • I'm aware I could easily add the class in the html code, but I pretend to do other jquery actions on my html. Things way more complex. And I need a platform for doing so. Commented Sep 7, 2018 at 9:11
  • the == $0 in chrome is explained here Commented Sep 7, 2018 at 9:18
  • Read up on the preferred way to create html using jQuery here Commented Sep 7, 2018 at 9:20
  • I'm pretty sure this could never work: $(this.mainDiv).addClass("puzzle"); since you are in a class and jQuery doesn't runs in that context. Commented Sep 7, 2018 at 9:21
  • oh, you are totally wrong. $(".template").clone() works like a charm inside my class. I think the root error is that things must be in the DOM before you can find them via $( __ ) but im still investigating. Commented Sep 7, 2018 at 9:23

1 Answer 1

2

You need to store the jQuery version of the div:

this.mainDiv = $(this.mainDiv).addClass("puzzle");

Otherwise you add the class to something that is discarded in the next line.

Taking into account what's discussed here:

var $div = $("<div>", {id: "foo", "class": "a"});

And Mark Baijens' comment:

It's common to prepend a dollar sign to variables that contain jQuery objects

You could do something like the following:

class Puzzle {
  constructor(ID, Name, Status) {
    this.ID = "main" + ID;
    this.Name = Name;
    this.Status = Status;
    this.$mainDiv = null;
  }

  generateDiv() {
    this.$mainDiv = $("<div>", {id: this.ID, "class": "puzzle"});
  }
}

$(document).ready(function() {
  var puzzle = new Puzzle(1, "MyPuzzle", 1);
  puzzle.generateDiv();
  $("#Puzzles").append(puzzle.$mainDiv);
})
.puzzle {
  width: 200px;
  height: 200px;
  background: black;
  border 1px solid white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Puzzles"></div>

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

6 Comments

simply instantiating the div as this.mainDiv = $('<div id="'+this.ID+'"/>'); instead of the original this.mainDiv = '<div id="' + this.ID + '"/>'; will also fix the thing
@javirs Best practice would be to change this.mainDiv to this.$mainDiv so you can easily see there is a jQuery object stored in that variable if you use that solution. It's common to prepend a dollar sign to variables that contain jQuery objects.
didnt event know $name was a valid variable name, Thank you SO much
@MarkBaijens I added your hint to my answer. Thanks!
@javirs I also updated my answer to show the use of best practices.
|

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.