jquery - How to find a sibling cell using class names? -


i have table that's generated looping through data, this:

$.each(array_rule_segments, function (key, listofwidgets) {     //console.log('in outer loop');     var widget_details = listofwidgets.split(',');     var counter = key + 1     htmlstring += '<tr id="listofwidgets_' + key + '">';     htmlstring += '<td><input type="button" value="remove" class="remove"/></td>';     htmlstring += '<td name="widget_type" class="widget_type" id="widget_type' + counter + '">' + widget_details[0] + '</td>';     htmlstring += '<td name="widget_details" class="widget_details"  id="widget_details' + counter + '">' + widget_details[1] + '</td>';     htmlstring += '<td name="widget_order" class="widget_order" id="widget_order' + counter + '">' + widget_details[2] + '</td>';     htmlstring += '<td name="messages" id="messages' + counter + '">' + widget_details[3] + '</td>';     htmlstring += '</tr>';  }); 

then later on, have logic checks see if new widget user trying add table exists. example, let's have widget in table value of "1234" in widget_details field.

i check so:

$("#add_to_table").live("click", function () {     var cell;     var result = $('#summary_table tr').find('td:contains(' + $('#input_widget_details').val() + ')'); //find cell in table same widget dteails information...           if (result.length > 0) {         //check duplicates using class names.              console.log(result.siblings(".widget_order").html());         if (result.siblings(".widget_order").html() == $('#widget_order').val() && result.siblings(".widget_type").html() == $('#widget_types').val()) {             alert("duplicate rule segment!");             return false;         }     } 

the first if statement works, finds row has matching widget details value but, i'm not able check sibling cells based on class names. debug statement have i'm trying check html() on sibling yields value of null.

i've tried .val() instead of .html().

here similar code mock up, 1 function shows how check html before added, other checks see if duplicates exist in table (what seem doing)

var array_rule_segments = ["widget1type,widgetdetails1,widgetorder1,widget1message", "widget1type,widgetdetails1,widgetorder1,widget1message"];  function rowexistsbeforeadd(row) {     var newrow = $(row);     var cell;     var foundsome;     var details = newrow.find('.widget_details').text();     console.log(details);     var result = $('#summary_table tr').find('td:contains(' + details + ')');      console.log("result:" + result.length);     // details match if true (has length) in incomming row     if (result.length > 0) {         // filter see if other things match in new row inputs         foundsome = result.filter(function () {             var mytype = $(this).find(".widget_type").text();             var myorder = $(this).find(".widget_order").text();             console.log("type:" + mytype);             console.log("order:" + myorder);             console.log(myorder == $('#widget_order').val());             console.log(mytype == $('#widget_types').val());             return myorder == $('#widget_order').val() && mytype == $('#widget_types').val();         });         if (foundsome.length) {             console.log('found' + foundsome.length);         }     }     return foundsome.length; }  function checkrow() {     var cell;     var foundsome;     var details = $('#input_widget_details').val();     console.log(details);     var result = $('#summary_table tr').find('td:contains(' + details + ')');     console.log("result:" + result.length);     if (result.length > 0) {         foundsome = result.parent().siblings().filter(function () {             var mytype = $(this).find(".widget_type").text();             var myorder = $(this).find(".widget_order").text();             console.log("type:" + mytype);             console.log("order:" + myorder);             console.log(myorder == $('#widget_order').val());             console.log(mytype == $('#widget_types').val());              return myorder == $('#widget_order').val() && mytype == $('#widget_types').val();         });         if (foundsome.length) {             console.log('found' + foundsome.length);         }     }     return foundsome.length; }  function addrow() {     $.each(array_rule_segments, function (key, listofwidgets) {         //console.log('in outer loop');         alert(listofwidgets.length);         var widget_details = listofwidgets.split(',');         var counter = key + 1;         var htmlstring = '';         htmlstring += '<tr id="listofwidgets_' + key + '">';         htmlstring += '<td><input type="button" value="remove" class="remove"/></td>';         htmlstring += '<td name="widget_type" class="widget_type" id="widget_type' + counter + '">' + widget_details[0] + '</td>';         htmlstring += '<td name="widget_details" class="widget_details"  id="widget_details' + counter + '">' + widget_details[1] + '</td>';         htmlstring += '<td name="widget_order" class="widget_order" id="widget_order' + counter + '">' + widget_details[2] + '</td>';         htmlstring += '<td name="messages" id="messages' + counter + '">' + widget_details[3] + '</td>';         htmlstring += '</tr>';         if (!rowexistsbeforeadd(htmlstring)) {             $(htmlstring).appendto('#summary_table');         }         /* late, added!         if (checkrow()) {             alert('duplicates');         }         */     }); }  $("#add_to_table").on("click", function () {     addrow(); }); 

fake markup:

<table id="summary_table"></table> <button id="add_to_table">add</button> <br/> <input type="text" id="widget_types" value="widget1type" /> <input type="text" id="widget_order" value="widgetorder1" /> <input type="text" id="input_widget_details" value="widgetdetails1" /> 

fiddle can see console logs etc works: http://jsfiddle.net/qtkpw/


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -