jQuery not finding parent

91 Views Asked by At

I'm trying to append something to a fieldset but ajax cannot find the parent of the button.

<fieldset id="asd">
        <legend>Export Model</legend>
       @Html.Label("Name") @Html.TextBox("name")
        <button onclick="addCategory()" type="button">Add Category</button>
    </fieldset>

function addCategory() {
            $(this).parent().append("test");
        }

I saw this example all over the site but it's not working for me. What did I miss?

1

There are 1 best solutions below

1
On BEST ANSWER

Change your html like this:-

<button onclick="addCategory(this)" type="button">Add Category</button>

And then in function:-

  function addCategory(btn) {
        $(btn).parent().append("test");
  }