1

Now: It makes hello button well, but when I click it, nothing happens.

Question:
I want to see alert or console.log

How can I do that?

My Code:

class ArtObj {
  constructor(string) {
    this.text = string
  }

  append_content(wrapper) {
    var div = $("<a id='hello' href='#'>hello</a>")

    div.find("#hello").on("click", function(e) {
      e.preventDefault()
      console.log(this.text)
      alert(this.text)
    });

    wrapper.append(div)
  }
}

art1 = new ArtObj("string_zz")
art1.append_content($("#artifact_wrapper"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="artifact_wrapper"> </div>

3
  • You can only bind the event after you appended it to the wrapper Commented Dec 16, 2020 at 13:20
  • @AlonEitan that's not correct. You can bind events to a jquery object before appending. You can't bind events to DOM elements that don't exist, but that's (subtly) different. Commented Dec 16, 2020 at 16:29
  • Oh I see it now, thanks for clarifying @freedomn-m and sorry for the late reply :) Commented Dec 17, 2020 at 7:24

3 Answers 3

3

You cannot do div.find because your div variable itself is an a tag with id hello. Just add event on variable div directly.

I would rename variable to a, so there is no confusion.

class ArtObj {

  constructor(string) {
    this.text = string
  }

  append_content(wrapper) {
    var div = $("<a id='hello' href='#'>hello</a>")

    div.on("click", function(e) {
      e.preventDefault()
      console.log(this.text)
      alert(this.text)
    });

    wrapper.append(div)
  }

}

art1 = new ArtObj("string_zz")
art1.append_content($("#artifact_wrapper"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="artifact_wrapper">

</div>

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

Comments

2
  • A better way to create a new jQuery Element is by passing the Element options as the second parameter

jQuery DOCS- Creating New Elements
As of jQuery 1.8, any jQuery instance method (a method of jQuery.fn) can be used as a property of the object passed to the second parameter

  • Define the click inside the Element options - no need to attach unnecessary listeners to other elements like "body" or document, etc.
  • Would be nice to define the this.$el right at init inside the constructor, that way, your append function would do exactly what it says.
  • Makes more sense to call a function by what it does, and that's something.append_to(somewhere), not append_content. Actually, you don't need that method at all if you already use jQuery.

class ArtObj {
  constructor(string) {
    this.text = string;
    this.$ = $("<a>", {
      id: "hello",
      text: "hello",
      href: "#",
      click: (e) => {
        e.preventDefault()
        console.log(this.text)
      }
    });
  }
}

const art1 = new ArtObj("string_zz");
art1.$.appendTo("#artifact_wrapper");
<div id="artifact_wrapper"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Since it makes no sense to create a class that generates a reusable component with a fixed ID, makes more sense to allow the user of your class to pass into the desired bits and pieces:

class ArtObj {
  constructor(options) {
    this.$ = $("<a>", options);
  }
}

const art1 = new ArtObj({
  id: "hello_1", // ID must be unique!
  text: "hello",
  href: "#",
  click: (e) => {
    e.preventDefault();
    console.log("string_zz");
  }
});
art1.$.appendTo("#artifact_wrapper");
<div id="artifact_wrapper"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

therefore, given the above, seems unnecessary to use a class at all:

$("<a>", {
  id: "hello_1", // ID must be unique!
  text: "hello",
  href: "#",
  click: (e) => {
    e.preventDefault();
    console.log("string_zz");
  },
  appendTo: "#artifact_wrapper"
});
<div id="artifact_wrapper"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

Comments

2

Check this. Your script was not in proper order and jQuery find function need to be used on parent elements.

class ArtObj {
  constructor(string) {
    this.text = string
  }

  append_content(wrapper) {
    wrapper.append("<a id='hello' href='#'>hello</a>");
    wrapper.find("#hello").on("click", function(e) {
      e.preventDefault()
      console.log(this.text)
      alert(this.text)
    });
  }
}

art1 = new ArtObj("string_zz")
art1.append_content($("#artifact_wrapper"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="artifact_wrapper"> </div>

Comments

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.