0

Any help with this would be greatly appreciated!

I'd like to add event listeners to the caretElement upon creation of the job object and in the event listener's function I'd like to reference some of the job object's properties e.g job_name or job_id.

This is so that when the caretElement is clicked I can forward the event on to other objects along with the job details.

I've tried using "this" and job.job_name.

function job(liElement) {
        this.liElement = liElement
        this.caretElement = liElement.getElementsByClassName("caret")[0]
        this.job_name = liElement.getElementsByTagName("span")[1].innerHTML
        this.job_id = liElement.getElementsByTagName("span")[1].getAttribute("id")

        this.caretElement.addEventListener("click", function() { 
            this.classList.toggle("caret-down");
            console.log(this); 
            console.log(job.job_name); 
        })
}

I don't get any errors but it just prints "undefined" to the console

Many Thanks

Nick
  • 161
  • 6
  • 2
    You might want to read this https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work (I think this is a duplicate but dont want to mod hammer it) – Jamiec Jun 24 '19 at 15:18
  • You need to provide a [mcve] – Quentin Jun 24 '19 at 15:24

0 Answers0