javascript tutorial - [Solved-5 Solutions] Select an element by name with jQuery - javascript - java script - javascript array



Problem :

How to select an element by name with jQuery?

Solution 1:

$(".bold").hide();  // selecting by class works
$("tcol1").hide();  // select by element name does not work

<tr>    
    <td>data1</td>
    <td name="tcol1" class="bold"> data2</td>
</tr>
<tr>    
    <td>data1</td>
    <td name="tcol1" class="bold"> data2</td>
</tr>  
<tr>    
    <td>data1</td>
    <td name="tcol1" class="bold"> data2</td>
</tr>
click below button to copy the code. By JavaScript tutorial team

Solution 2:

$('td[name=tcol1]') // matches exactly 'tcol1'

$('td[name^=tcol]') // matches those that begin with 'tcol'

$('td[name$=tcol]') // matches those that end with 'tcol'

$('td[name*=tcol]') // matches those that contain 'tcol’
click below button to copy the code. By JavaScript tutorial team

Solution 3:

var value = $("[name='nameofobject']");
click below button to copy the code. By JavaScript tutorial team

Solution 4:

jQuery("input[name='mycheckbox']").each(function() {
  console.log( this.value + ":" + this.checked );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="mycheckbox" value="11" checked="">
<input type="checkbox" name="mycheckbox" value="12">
click below button to copy the code. By JavaScript tutorial team

Solution 5:

function toggleByName() {
  var arrChkBox = document.getElementsByName("chName");
  $(arrChkBox).toggle();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <head>
    <title>sandBox</title>
  </head>
  <body>
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="radio" name="chName"/><br />
    <input type="button" onclick="toggleByName();" value="toggle"/>
  </body>
</html>
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Select an element by name with jQuery