0

Im building a website at the moment and and i have a problem about getting the div id on the button click where div button contains button.

<div id="div" class="tab-content" title="Foods">
  <div class="container">
    <div class="row">
      <div class="col-lg-2">
        <div class="tabs-block">
          <asp:Button ID="btnSend" runat="server" OnClick=""/>
         </div>
      </div>
    </div>
   </div>
  </div>
 </div>

This is the code which is not my code but similar. What i am trying to that is when i click the btnSend button i need to get the tab-contents id and the title which is "food", im trying to take the title with javascript and jquery and after that im gonna use it on my background code which is

 protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                string message = "";
                message += "<p>"Title here" Resevation Form </p><br />";
            {
        }

i need the use that title on the background code which is "Title here"

Enes a.
  • 5
  • 4
  • If you are using jquery try in btnSend_Click function: $(sender).parent().parent().parent().parent().parent().attr("id") to get id and $(sender).parent().parent().parent().parent().parent().attr("title") to get title. This may help you. – Parth Patel Jul 16 '19 at 12:19
  • @ParthPatel OP didn't say anything about the use of jQuery... also if jQuery is accepted, then using `closest()` would be a better idea than a lot of `parents()` – Calvin Nunes Jul 16 '19 at 12:21
  • With Jquery use the `closest` method `$(this).closest('.tab-content');` Do you want to use pure JavaScript? – daddygames Jul 16 '19 at 12:27
  • JavaScript has a solution like so (beware no IE support in this example)... https://stackoverflow.com/questions/22119673/find-the-closest-ancestor-element-that-has-a-specific-class – daddygames Jul 16 '19 at 12:30

1 Answers1

1

You should write something like this:

<div class="col-lg-2">
    <div class="tabs-block" name="Title">
      <Button ID="btnSend" runat="server"/>
     </div>
  </div>

$( "#btnSend" ).click(function(event) {
  console.log($(this).parent().attr("name"))
});

In $(this).parent().attr("name") you will have the name of your clicked tab.

Iiskaandar
  • 394
  • 2
  • 7
  • OP is not using or didn't mentioned the use of jQuery, apparently he's using asp.net with a C# code to make the button click event. – Calvin Nunes Jul 16 '19 at 21:19