0

I'm building a web form that populates dynamically from a stored procedure. It is a table with a dropdown list and a text box and one label. I can successfully grab the data from the dropdown and the text box, but I cannot grab the data from the label. Here is the code that populates the data section of the table.

        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'><select name='select_" + count + "'><option value='Pending'>Pending</option><option value='Approve'>Approve</option><option value='Deny'>Deny</option></select></td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'><input type='text' name='comments_" + count + "' /></td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + locationNum + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + shipToNum + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + address + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + city + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + state + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + zip + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + soldToName + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + exemptionDescription + "</td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'><label for='select_" + count + "' name='label_" + count + "'>" + exemptionRequestDetailID + "<label></td>");
        sb.Append("<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + reason + "</td>");

Here is the code that grabs the needed data from the table in the aspx page and sends it to the codebehind for manipulation.

function SendForm() {
        var count = 0;
        var elLength = form1.elements.length;
        for (i = 0; i < elLength; i++) {
            var type = form1.elements[i].type;
            alert(type);
            if ((type == "select-one") && (form1.elements[i].value != "Pending")) {
                count++
            }
        }
        var data = new Array(count);
        var text = new Array(count);
        var exempID = new Array(count);
        for (i = 0; i < elLength; i++) {
            var type = form1.elements[i].type;
            if ((type == "select-one") && (form1.elements[i].value != "Pending")) {
                data[i] = form1.elements[i].value;
            }
            if ((type == "text") && ((form1.elements[i-1].value == "Approve") || (form1.elements[i-1].value == "Deny"))) {
                text[i] = form1.elements[i].value;
            }
            if ((type == "label") && ((form1.elements[i-2].value == "Approve") || (form1.elements[i-2].value == "Deny"))) {
                alert(form1.elements[i].value);
            }


        }


        PageMethods.SendForm(data, text, OnSucceeded, OnFailed);
    }

I have alerts set up to check if it is grabbing the label values, but it is showing nothing at all for the label. Any help would be appreciated.

2
  • 2
    Why not use a Repeater and throw in some css classes to make things easier to find? Commented Jan 4, 2012 at 19:47
  • Regardless of how the markup is created - though I agree that's not how I'd do it - adding some CSS classes would certainly help Commented Jan 4, 2012 at 19:55

3 Answers 3

1

If I read this correctly, you're looking for the inner text (below, locationNum), rather than the label, of an element like this:

<td style='border-top: 1px solid black; border-left: 1px solid black;'>" + locationNum + "</td>

If that's the case, take a look at firstChild.nodeValue of the td element.

var table_children = document.getElementsByTagName( 'td' );
var x = 0, len = table_children.length;
for( x = 0; x < len; x += 1 ) {
     var tag_text = table_children[ x ].firstChild.nodeValue;   
     alert( tag_text );
}

No jQuery required. Interact with this code here.

Sign up to request clarification or add additional context in comments.

Comments

1

Give each row a unique class name. Give your table one too:

sb.Append("<table class='table1'...
sb.Append("<td class='dataRow row1' ......

Then you can find all of the elements in the table easily using jQuery:

$('.table1 td').each(function() {
    var myClass = $(this).class
    selectVal = $(myClass).find('select').val()
    textVal = $(myClass).find('input').val()
    labelVal = $(myClass).find('label').html() 
    ...your other logic ...
})

Comments

0

The label html tag is just that, a label. It does not have a value attribute like a textarea or select tag. This is why you can not get the value from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.