Quoting via html table using jQuery

I have an html table and I want to basically scroll through each row and each cell in the row and just print the results. One thing is that some of the cells have input fields, some of them have drop-down lists, and some have raw content inside TD.

What is the fastest way to just looop through each cell in the html table and get the result of each cell. to select the dropdowns, I would like to capture the select value (and not the text on the screen).

+3
source share
4 answers

Online demo: http://jsbin.com/ewazu

TD , first-child input. , . , , . , . , HTML TD:

$("table td").each(function(i,o){
  var value = ( $(":first-child", this).is(":input") ) 
    ? $(":first-child", this).val() 
    : ( $(this).text() != "" ) 
      ? $(this).text() 
      : $(this).html() ;
  alert(value);
});
+6

, - :

$("#myTable tr").each(function() {
    // this represents the row
    $("td > *", this).each(function() {
        // nodeName attribute represents the tag - "INPUT" if element is <input>
        // use the type attribute to find out exactly what type 
        // of input element it is - text, password, button, submit, etc..
        if(this.nodeName == "INPUT") {
            console.log($(this).attr("type"));
        }
    });
});

#myTable > tr , <tr> id="myTable".

, td > * <td>, this . CSS, DOM. jQuery docs.

jQuery core docs - , jQuery.

+2

, , , td , td , - : http://jsbin.com/alara3/edit

+2

.

$(':input', '#tableId').val('');
-1

Source: https://habr.com/ru/post/1728696/


All Articles