Vijikumar M
Vijikumar M

Reputation: 3764

How to extract a td from html

I have one variable its having some html data. I want to extract one td from that html.

If I give alert(returnData) its giving the following output.

<tr class='addedrow'>
    <td id="abc">DOM-001
        <input id="bill_details_1_narration" name="bill_details[1][narration]" type="hidden" />
    </td>
    <td>DOMSTAL O CAPSULES</td>
    <td>
        <select id="item_details_batch_number" name="item_details[batch_number]">
            <option value="47">BATCH-830</option>
        </select>
    </td>
    <td>
        <input id="bill_details_quantity" name="bill_details[quantity]" size="2" type="text" value="2.0" />
    </td>
    <td>
        <input id="item_details_cost_price" name="item_details[cost_price]" size="2" type="text" value="23391.0" />
    </td>
</tr>

Here I want to extract the value 1 from bill_details_1_narration.

How do I achieve this?

Upvotes: 0

Views: 186

Answers (3)

kidwon
kidwon

Reputation: 4524

If returnData is HTML node that should work:

var bill_details = $(`#bill_details_1_narration`,returnData).val();

But since you alert it I it's string, why is it so? How do you return that data?

UPDATE:

var valArray = [],
billDet = $('input[name$="[narration]"]', returnData);

billDet.each(function () {
    valArray.push($(this).val())
});

Upvotes: 2

Francois Borgies
Francois Borgies

Reputation: 2408

You can transfer value from one element to another like this:

$(function(){
    $('#id_Expeditor').html($('#id_Receiver').html());
});

Upvotes: 2

Darren
Darren

Reputation: 70746

The title of the question refers to the td but from my understanding I believe you want to retrieve 1 from the name attribute since 1 does not appear anywhere else in your markup:

If you want to retrieve the value from:

bill_details[1][narration]

Firstly I'd suggest using data-attributeName

You could then use this code to extract 1 from the attribute.

var obj = $("#bill_details_1_narration");
var attr = obj.attr("name");
var sub = attr.substring(attr.indexOf('[') +1, attr.indexOf("]"));

alert(sub);

http://jsfiddle.net/t8uFC/

Upvotes: 0

Related Questions