Reputation: 1413
This is a Fiddle of my site page: https://jsfiddle.net/e1bhwvj8/1/
I want to clear the information of the form on the currently active tab via a button press.
The tabs each have a unique id, as do their forms, but only the currently active one will have the "active" class assigned to it.
<div class="tab-content">
<div id="general" class="tab active">
<form id="generalForm">
<div id="tabTableContainer">
<table style="width:60%">
<tr>
I'm wondering how I can detect which div has the "active" class and then get the div id, so I can then know which form to reset.
Upvotes: 0
Views: 1368
Reputation: 21465
Check this updated fiddle.
So I have changed your function resetTab
to this:
function resetTab() {
jQuery(".tabs div.active form")[0].reset();
}
The selector finds the div
with class active
among the other tabs inside the .tabs
div. Then inside the active tab(div) it finds the form
. The [0]
means that it is using the first element of the returned collection(you may want to put an if
in case of no form is found) and apply reset()
to it.
I think you can try loop through jQuery(".tabs form")
and apply reset()
to all to perform a clearAll function, but I didn't tried it.
Note: I have changed your fiddle script setting to "wrap in < head >" instead of "onLoad" or your code would never work.
Upvotes: 2
Reputation: 3367
You can use basic functions to get the id of an element.
document.getElementsByClassName('active')[0].id
Upvotes: 0
Reputation: 1137
Since you're using jQuery you can select the div element then select one that has the active class as:
var thisID = $('div.active').attr('id')
This will accurately get the specific ID assuming you only have one div on the page with an active class. If you have multiple you may need to perform some kind of loop.
Upvotes: 0