Add Content after click Button

Manuel0815

Newbie
Joined
Jun 12, 2021
Messages
3
Reaction score
1
Hello,

First i want to explain what i want to do.
I have done a website, and in the main part is a section with different Maindivs. Those maindivs have all the same css and html code with a lot of divs in those maindivs. Here for excample:

<section class="_CB11">
<div class="Maindiv>
<div>
<div>
<button></button>
</div>
</div>
</div>
<div class="Maindiv">
same procedure like first Maindiv...
</div>

</section>

My Question:
How can i get the nthChild of the Maindiv when i'm click the button?
 
I think you asked the wrong forum. I suggest you to find answers on stackoverflow.
Also maybe you should add class or id for each div so you can get which div you want to get?
 
If you are using jQuery, try something like:
JavaScript:
//I didn't validate this but it should give you the idea
//If you use jQuery

$("._CB11 button").click(function(e){
    var nthChild = e.target;
    //nthChild would be the HTML element of the exact button that was clicked
});
 
It's usually easier if you explain why you wanna do that - and we can come with a fast and easy solution.
People that aren't into coding usually look at code and overthink it.

In the example you provided - if I'm reading it well - you're looking at it wrong.
Code:
$(this).parent()
should do the job.
 
I think you asked the wrong forum. I suggest you to find answers on stackoverflow.
Also maybe you should add class or id for each div so you can get which div you want to get?
We have many users here who can easily answer these questions. SOF is good, but BHW is enough for programming related questions in general. So... Don't discourage other members from asking questions please.


OP, try this jsfiddle snippet out and let me know if it solves your problem.

Code:
https://jsfiddle.net/ptdj7g2b/9/

In a nutshell, i think you are looking for something like this:

Code:
 $('.Maindiv button').click(function(){
       alert($('.Maindiv:last-child').text());
});

If not, please explain the problem a bit more.

P.s. your html is messed up. May be fix that first.
 
Back
Top