0

I have a form with a class of "foo" on my page. I'd like to remove that specific form with the click of a button.

<div onclick="delete_me(foo)">
    <button>Delete</button>
</div>   


<script>
    function delete_me(formname) {
        document.getElementsByClassName(formname).remove();
    }
</script>

When I click the button, nothing happens. Do you know where I've gone wrong?

Thanks!

Jason Howard
  • 1,464
  • 1
  • 14
  • 43
  • `document.getElementsByClassName(formname)[0].remove();` – Mamun Jul 05 '18 at 08:45
  • 1
    "nothing happens" — Open the Console of your browser's developer tools. Read the error message. – Quentin Jul 05 '18 at 08:46
  • `foo` is a undefined variable. Did you mean a string instead? - Also, this question -might- be a duplicate at one stage, but it isn't now. The primary issue here is that `formname=undefined` at the moment. – Caramiriel Jul 05 '18 at 08:46
  • Mamun is correct. I've added [0]. foo is actually a string that is equal to "id_form-2-id". I'm now getting an error of the following in the console: Uncaught ReferenceError: id_form is not defined. Is it dropping the "-2-id" from the variable that being passed into the function? – Jason Howard Jul 05 '18 at 09:18
  • I hard coded a variable in my js. I've got this: document.getElementsByClassName[0]("id_form_2_id").remove(); The console still returns an error saying that id_form is not defined... I'm baffled. – Jason Howard Jul 05 '18 at 09:20
  • @ Caramiriel I'm likely missing something obvious, but here are my thoughts. formname is a perimeter that is being passed into the delete_me function by the onclick call. foo is a string, not a variable. Is there some other way that I need to indicate that foo is a string within the onclick function? – Jason Howard Jul 05 '18 at 09:28

0 Answers0