javascript - jQuery child elements with same ID -


imagine have html structure:

    <div id="bodyread">           ...             <div id="list"></div>     </div>     ...      <div id="bodywrite">           ...             <div id="list"></div>     </div> 

as can see, have #list inside 2 different divs (bodyread , bodywrite). lighter code, preferred work id instead of class (even knowing duplicate ids isn't valid, code makes more sense me), , selecting #list on jquery this:

$('#bodyread #list').off('click').on('click', function() ... $('#bodywrite #list').off('click').on('click', function() ... 

works. i'm taking care of make duplicate ids on child divs, can separate them parent div on jquery selector.

is approach wrong? mean, can me in trouble?

don't duplicate id in child element .. change class="list" can use

$('.list').on('click',function(){     var getparent = $(this).parent().attr('id');     alert(getparent); }); 

and if class="list" not first level child of of #bodyread or #bodywrite

add class it

 <div id="bodyread" class="maindiv">  <div id="bodywrite" class="maindiv"> 

and use

$('.list').on('click',function(){     var getclosest = $(this).closest('.maindiv').attr('id');     alert(getclosest); }); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -