Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parentNode.getElementById() is not working

I encountered a problem while practicing html. When I used parentNode in JavaScript, I thought it is not hard to treat.

But to get some element under parentNode using getElementById or another function is not working as my thinking.

var this_question = selectObj.parentNode.parentNode;
    alert(this_question); //it is working perfectly
    alert(this_question.getElementById('question'))

It's not working. I can't understand...

<script>
function addQuestion(selectObj) {
    var this_question = selectObj.parentNode.parentNode;
    alert(this_question); //it is working perfectly
    alert(this_question.getElementById('question')) //It's not working. I can't understand..
}
</script>

<ol id="question_list">
    <li>
        <textarea class="question" name="question" id="question"></textarea>
        <select name="question_type" id="question_type" onChange="javascript:selectEvent(this)">
            <option>-----</option>
            <option value="text" >단답형</option>
            <option value="paragraph" >서술형</option>
            <option value="multiple_choice">다지선</option>
            <option value="checkbox">다중선택</option>
            <option value="scale">scale</option>
        </select>   

        <div id='answer_div'><p>부가설명:<input name='top_label' id='top_label' type='paragraph' /></p> <p>답변:<input name='answer_text' id='answer_text' type='text' /></p></div>

        <p>
            <input type="button" value="Add Question" onclick="javascript:addQuestion(this)"/>
            <input type="button" value="Done" onclick="javascript:finish()"/>
         </p>
    </li>
</ol>
like image 689
Jeff Gu Kang Avatar asked Dec 22 '22 08:12

Jeff Gu Kang


1 Answers

getElementById() is a method of documents, not available in elements.

You may use:

this_question.getElementsByTagName('textarea')[0]

getElementsByTagName() is available in elements.

like image 52
Dr.Molle Avatar answered Dec 24 '22 02:12

Dr.Molle