How to find if div with specific id exists in jQuery?

后端 未结 10 1320
我在风中等你
我在风中等你 2020-11-28 17:51

I’ve got a function that appends a

to an element on click. The function gets the text of the clicked element and assigns it to a variable called
10条回答
  •  我在风中等你
    2020-11-28 18:14

    Nick's answer nails it. You could also use the return value of getElementById directly as your condition, rather than comparing it to null (either way works, but I personally find this style a little more readable):

    if (document.getElementById(name)) {
      alert('this record already exists');
    } else {
      // do stuff
    }
    

提交回复
热议问题