javascript - how to get element value by knowing parent's name of another parent -
let's have 2 textareas same id , parents have same id, parent forms different names.. , need value of second textarea of second form!!! here's simple code..
var formx = document.getelementsbyname("form2"); for(i = 0; < formx.length; i++){ if(formx[i].type == "textarea"){ var inputval = formx.value; alert(inputval); } }
<form name="form1"> <div id="samediv1"> <div id="samediv2"> <textarea id="sameid">value1</textarea> </div> </div> </form> <form name="form2"> <div id="samediv1"> <div id="samediv2"> <textarea id="sameid">value2</textarea> </div> </div> </form>
- when tried it, no alert appears, , know because loop didn't find text areas because inside element(s) within form.. so, how go deep in parents , childs?!!
thanks lot
you can same using jquery find()
or children()
method. here can read jquery traversing.
console.log($('[name=form2]').find('#sameid').text()); //or value() can used in place of text() method
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form name="form1"> <div id="samediv1"> <div id="samediv2"> <textarea id="sameid">value1</textarea> </div> </div> </form> <form name="form2"> <div id="samediv1"> <div id="samediv2"> <textarea id="sameid">value2</textarea> </div> </div> </form>
Comments
Post a Comment