Rajasekhar
Rajasekhar

Reputation: 2455

How to skip the rows using jquery :not

I stuck the issue for skipping row using jQuery :not. Here I adding rows dynamically and have some <tr> have one class addBgColor. Once we click on submit I would like to convert all table data to the xml strucuted data except the row contains "addBgColor" class, here is the code what I tried, please help me

var elements = $(".multipleData tr:not(.addBgColor) td input:checked");
    var size = elements.size();
    bodyRowSet = bodyRowSet + "<count>" + size + "</count>";
    elements.each(function() {    
            var tdValue = $(this).parent().next('td').html().trim();        
        bodyRowSet = bodyRowSet + "<row><eventid>";          
            bodyRowSet = bodyRowSet +  tdValue;
                bodyRowSet = bodyRowSet + "</eventid></row>";
    });

Upvotes: 0

Views: 337

Answers (2)

Tejas Patil
Tejas Patil

Reputation: 52

As your jsfiddle code please update the HTML code for the table was not correct please update to as below:

 <table class="multipleData" style="border:1px solid #000000;">


<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test1
    </td>
  <td>
</tr>
<tr  class="addBgColor">
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test2
    </td>
  <td>
</tr>

<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test3
    </td>
  <td>
</tr>

<tr>    
    <td>
        <input type="checkbox" id="" name=""/>
    </td>
    <td>
        test4
    </td>
</tr>

 </tbody>

Upvotes: 0

Gvidas
Gvidas

Reputation: 1964

Correct your html nesting and everything will run just fine. There's no tbody start tag & all tr's doesnt' have endings. Here's fixed jsFiddle.

$(document).ready(function(){
    $("#idXmlData").click(function(){
    var xmlStart =  "<?xml version=\"1.0\" encoding=\"UTF-8\">";
    var xmlFirstRowSet = "<event>";
	  var xmlLast="</event>";
    var headerRowSet='';
    xmlFirstRowSet = xmlFirstRowSet + headerRowSet;
    var bodyRowSet ='';
    
    var elements = $(".multipleData tr:not(.addBgColor) td input:checked");
    var size = elements.size();
    bodyRowSet = bodyRowSet + "<count>" + size + "</count>";
    elements.each(function() {    
    		var tdValue = $(this).parent().next('td').html().trim();        
        bodyRowSet = bodyRowSet + "<row><eventid>";          
            bodyRowSet = bodyRowSet +  tdValue;
        		bodyRowSet = bodyRowSet + "</eventid></row>";
    });
    
    var xml = xmlStart + xmlFirstRowSet + bodyRowSet+xmlLast;
     window.alert(xml);  	
    });
  });
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="multipleData" style="border:1px solid #000000;">

<tbody>
	<tr>	
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test1
		</td>
      </tr>
	<tr  class="addBgColor">
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test2
		</td>
      </tr>
	<tr>
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test3
		</td>
    </tr>
	<tr>	
		<td>
			<input type="checkbox" id="" name=""/>
		</td>
		<td>
			test4
		</td>
	</tr>

      
     </tbody>
</table>

  <input type="button" name="" value="XML Data" id="idXmlData" />

Upvotes: 3

Related Questions