2

I have some code :

    <table>
        <tbody>
            <tr>
                <td id='myTdId'>
                    <input type='text' value='some value' />
                </td>
            </tr>

            ...

        </tbody>
    <table>

I want to get innerHTML of myTdId td element and get value of text input. Is it possible?

for example :

var tdInnerHTML = document.getElementById('myTdId').innerHTML;
// convert this to element operation and access to value of it ... 
console.log(tdInnerHTML.value); // :(

please help me, THANKS...

EDIT : I do not access to input text!

jamshid
  • 225
  • 5
  • 12

6 Answers6

3

How about using childNodes to navigate?

It is an array containing nodes of contained elements.

In your case...

var tdInputVal = document.getElementById('myTdId').childNodes[0].value;
console.log(tdInputVal ); // :(
dev8080
  • 3,950
  • 1
  • 12
  • 18
2

Can you add Id on the input itself?

<table>
    <tbody>
        <tr>
            <td id='myTdId'>
                <input type='text' value='some value' id="myInput" />
            </td>
        </tr>

        ...

    </tbody>
<table>

Then access it via

var input = document.getElementById('myInput').value;

Or access it via tag

var input = document.getElementsByTagName("input")[0].value;
Bk Santiago
  • 1,523
  • 3
  • 13
  • 24
1

Alternative clear way is to have id or class on your HTML so you can get inner value easily.

For example

HTML:

<input id="text-input" value="">

Javascript

var inputValue = document.getElementById('text-input').value;
console.log(inputValue);

jQuery version

var inputValue = $('#text-input').val();

Hope this helps.

Kaung Myat Lwin
  • 1,049
  • 1
  • 9
  • 23
1

I dont think you need to call innerHTML on that element. You need to get child of the td element with id "myTdId". To do that you can use

var tdElement = document.getElementById('myTdId');
console.log(tdElement.children[0].value);

This should get you the value of the td Element without the need of setting id or class to the td element.

I am assuming you have only one element inside the td;

I did not test the code but it should work.

1

Use getElementById than use on result object also getElementBy_XXX . If you use getElementsByClassName or TagName you will get array of child element.

//ELEMENT
var tdElement = document.getElementById('myTdId');

// INNER HTML
var tdInnerHTML = document.getElementById('myTdId').innerHTML;

var childElement = tdElement.getElementsByTagName("input");

//if td is only and always first element than we can use  : 
//childElement[0]

alert(childElement[0].value)
<table>
        <tbody>
            <tr>
                <td id='myTdId'>
                    <input type='text' value='some value' />
                </td>
            </tr>

       

        </tbody>
    <table>
    
    <script>
    
    //  I dont use this function but you can if you want
    function getElementInsideElement(baseElement, wantedElementID) {
    var elementToReturn;
    for (var i = 0; i < baseElement.childNodes.length; i++) {
        elementToReturn = baseElement.childNodes[i];
        if (elementToReturn.id == wantedElementID) {
            return elementToReturn;
        } else {
            return getElementInsideElement(elementToReturn, wantedElementID);
        }
    }
}
    
    </script>
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
1
// Use querySelector
var input = document.querySelector('#myTdId input');
var val = input.value;