2

How to delete an element when I click it

HTML

<span id="portoDeleteFIle_${id}" class="deleteFile" title="Delete">+</span>

//${id} is number and < 99

JS

<script type="text/javascript">
jQuery("span[id^='portoDeleteFIle_']").click(function(e) {
      id = e.currentTarget.attributes.id.value.replace("portoDeleteFIle_", "");
      console.log(id);
});
</script>
Dev Minty
  • 64
  • 6
  • need a jsfiddle. and what error do you get in console? – chovy Apr 28 '22 at 03:36
  • I would suggest you give more information as to what you're trying to achieve. Post title says something else and content says something else, also there's no code in your example to actually delete an html element. – Sang Suantak Apr 28 '22 at 03:40
  • it simply not working.I'm just trying to get it to run js – Phung Thuan Apr 28 '22 at 04:51
  • 1
    Your code [works fine](https://jsfiddle.net/vfdej725/). Open the browser console (F12, select console) and check for any errors, such as `jQuery not defined`. *If* your `span` is generated *after* your js runs, then either: 1) wrap the click event in doc.ready (see [learn jquery doc ready](https://learn.jquery.com/using-jquery-core/document-ready/) or 2) use event delegation (see this [question](https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements)) – freedomn-m Apr 28 '22 at 07:56

1 Answers1

0

Try this:

jQuery("span[id^='portoDeleteFIle_']").click(function(e) {
      id = e.currentTarget.attributes.id.value.replace("portoDeleteFIle_", "");
      console.log(id);
});

There was missing ) in your code.

Arif Khan
  • 508
  • 5
  • 12
  • use a text edtior with some javascript extensions and errors like this will be highlighted, like vscode – Rabbit Apr 28 '22 at 04:15