Question: Div parent and two children, get id of one children in other children

Question

Div parent and two children, get id of one children in other children

Answers 3
Added at 2017-01-05 12:01
Tags
Question

I'd like get the id of my svg in jquery , I try this jquery but it's undefined.

$(event.target).children('svg').attr("id")

Target.event return data of p, when I look inside parent I have mmy column :

<div class="col-md-4">
  <div class="zone" >
          <svg id="graphItem0_2" with="300" height="300"></svg>
          <p class="glyphicon glyphicon-chevron-down" style="position: absolute;  top: 300px; right:20px;" data-toggle="collapse" data-target="#demo2"></p>
    </div>
</div>

I have many svg element in my page when I try $(svg).attr("id"), I get just the first element but when I click on a other svg it's the same id then my other svg have not the same id.

Answers to

Div parent and two children, get id of one children in other children

nr: #1 dodano: 2017-01-05 12:01

You could attach event click to the parent div zone then search for the svg using jQuery object $(this) instead :

$('.zone').on('click',function(){
  alert( $(this).find('svg').attr("id") );
})

Hope this helps.

$('.zone').on('click',function(){
  alert( $(this).find('svg').attr("id") );
})
.zone{
  background-color: red;
}
svg{
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="zone" >
  <svg id="graphItem0_2" with="300" height="300"></svg>
  <p class="glyphicon glyphicon-chevron-down" style="position: absolute;  top: 300px; right:20px;" data-toggle="collapse" data-target="#demo2"></p>
</div>

nr: #2 dodano: 2017-01-05 12:01

Can you please try this :-

var x = document.getElementById("svg").id;

It will work good if there is only 1 svg element on page.

nr: #3 dodano: 2017-01-05 12:01

Try to something Like this.

$(".zone").click(function(){
  var id = $("svg").attr("id");
  alert(id);
});
Source Show
◀ Wstecz