jQuery Selector How to - Locate element by name inside parent element








Question

We would like to know how to locate element by name inside parent element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<style type='text/css'>
#id2 .mycolor {<!--from  www  .j  ava2  s .  c  o m-->
  color: white;
  background-color: red;
}

#id3:active {
  color: red;
  background-color: white;
}
</style>
<script type='text/javascript'>
$(function(){
    $('#id2').find('span').addClass('mycolor');
});
</script>
</head>
<body>
  <div id="id2">
    <span>Demo</span>
  </div>
</body>
</html>

The code above is rendered as follows: