Simple way to get element by id within a div tag?

后端 未结 8 529
忘了有多久
忘了有多久 2020-12-04 19:44

Please forgive me if I repeat the question.

I have HTML that all elements inside a div tag has different id, suppose I have already get the reference to the div, is

相关标签:
8条回答
  • 2020-12-04 20:06

    You may try something like this.

    Sample Markup.

    <div id="div1" >
        <input type="text" id="edit1" />
        <input type="text" id="edit2" />
    </div>
    <div id="div2" >
        <input type="text" id="edit3" />
        <input type="text" id="edit4" />
    </div>
    

    JavaScript

    function GetElementInsideContainer(containerID, childID) {
        var elm = {};
        var elms = document.getElementById(containerID).getElementsByTagName("*");
        for (var i = 0; i < elms.length; i++) {
            if (elms[i].id === childID) {
                elm = elms[i];
                break;
            }
        }
        return elm;
    }
    

    Demo: http://jsfiddle.net/naveen/H8j2A/

    A better method as suggested by nnnnnn

    function GetElementInsideContainer(containerID, childID) {
        var elm = document.getElementById(childID);
        var parent = elm ? elm.parentNode : {};
        return (parent.id && parent.id === containerID) ? elm : {};
    }
    

    Demo: http://jsfiddle.net/naveen/4JMgF/

    Call it like

    var e = GetElementInsideContainer("div1", "edit1");
    
    0 讨论(0)
  • 2020-12-04 20:12

    A simple way to do what OP desires in core JS.

    document.getElementById(parent.id).children[child.id];
    
    0 讨论(0)
  • 2020-12-04 20:16

    In HTML ids should be unique. I suggest you change your code to something like this:

    <div id="div1" >
        <input type="text" name="edit1" id="edit1" />
        <input type="text" name="edit2" id="edit2" />
    </div>
    <div id="div2" >
        <input type="text" name="edit1" id="edit3" />
        <input type="text" name="edit2" id="edit4" />
    </div>
    
    0 讨论(0)
  • 2020-12-04 20:19
    Sample Html code   
     <div id="temp">
            F1 <input type="text" value="111"/><br/>
            F2 <input type="text" value="222"/><br/>
            F3 <input type="text" value="333"/><br/>
            Type <select>
            <option value="A">A</option>
            <option value="B">B</option>
            <option value="C">C</option>
            </select>
            <input type="button" value="Go" onclick="getVal()">
        </div>
    
    Javascript
    
        function  getVal()
        {
            var test = document.getElementById("temp").getElementsByTagName("input");
            alert("Number of Input Elements "+test.length);
            for(var i=0;i<test.length;i++)
            {
              if(test[i].type=="text")
              {
                alert(test[i].value);
              }
           }
          test = document.getElementById("temp").getElementsByTagName("select");
          alert("Select box  "+test[0].options[test[0].selectedIndex].text);
        }
    
    By providing different tag names we can get all the values from the div.
    
    0 讨论(0)
  • 2020-12-04 20:20

    You don't want to do this. It is invalid HTML to have more than one element with the same id. Browsers won't treat that well, and you will have undefined behavior, meaning you have no idea what the browser will give you when you select an element by that id, it could be unpredictable.

    You should be using a class, or just iterating through the inputs and keeping track of an index.

    Try something like this:

    var div2 = document.getElementById('div2');
    for(i = j = 0; i < div2.childNodes.length; i++)
        if(div2.childNodes[i].nodeName == 'INPUT'){
            j++;
            var input = div2.childNodes[i];
            alert('This is edit'+j+': '+input);
        }
    

    JSFiddle

    0 讨论(0)
  • 2020-12-04 20:23
    var x = document.getElementById("parent").querySelector("#child");
    // don't forget a #
    

    or

    var x = document.querySelector("#parent").querySelector("#child");
    

    or

    var x = document.querySelector("#parent #child");
    

    or

    var x = document.querySelector("#parent");
    var y = x.querySelector("#child");
    

    eg.

    var x = document.querySelector("#div1").querySelector("#edit2");
    
    0 讨论(0)
提交回复
热议问题