最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get html element inside another element in javascript - Stack Overflow

programmeradmin1浏览0评论

Hi I am creating dynamic div elements , let say 4 div are there . div1,div2,div3,div4. each div has set of radio buttons rad1,rad2,rad3,rad4 . see as follows.

div1 ->rad1,rad2,rad3,rad4 groupName=rd1 ->end div1
div2 ->rad1,rad2,rad3,rad4 groupName=rd2 ->end div2
div3 ->rad1,rad2,rad3,rad4 groupName=rd3 ->end div3
div4 ->rad1,rad2,rad3,rad4 groupName=rd4 ->end div4

This is dynamically created html. i want to get which Radio selected from div2 or div3 what script i have to write ?

Hi I am creating dynamic div elements , let say 4 div are there . div1,div2,div3,div4. each div has set of radio buttons rad1,rad2,rad3,rad4 . see as follows.

div1 ->rad1,rad2,rad3,rad4 groupName=rd1 ->end div1
div2 ->rad1,rad2,rad3,rad4 groupName=rd2 ->end div2
div3 ->rad1,rad2,rad3,rad4 groupName=rd3 ->end div3
div4 ->rad1,rad2,rad3,rad4 groupName=rd4 ->end div4

This is dynamically created html. i want to get which Radio selected from div2 or div3 what script i have to write ?

Share Improve this question edited Feb 8, 2011 at 11:19 Red Swan asked Feb 8, 2011 at 11:02 Red SwanRed Swan 15.5k44 gold badges161 silver badges241 bronze badges 4
  • 1 Please provide sample code showing us what you're working with and what you already have accomplished. What counts as a selected option? Do you have a <select> element in each div, with <option> children? – outis Commented Feb 8, 2011 at 11:07
  • google 'option element html', the first results' first sentence states 'The option element goes inside the select element' – BiAiB Commented Feb 8, 2011 at 11:08
  • Eidted Question.Sorry it is not option , it is Radio buttons . and code is so big . so i just explain the scenario – Red Swan Commented Feb 8, 2011 at 11:20
  • even if the production code is massive, you should be able to create minimal sample code. – outis Commented Feb 9, 2011 at 13:04
Add a comment  | 

4 Answers 4

Reset to default 7
  • DEMO: http://so.lucafilosofi.com/get-html-element-inside-another-element-in-javascript
function checkRadio() {
    var radios = document.getElementById('rBox').getElementsByTagName('input');
    for (var i = 0; i < radios.length; i++) {
        radios[i].onclick = function(e) {
            var pid =  this.parentNode.id;
            if ( pid == 'group-2' || pid == 'group-3' )
                alert('PARENT: ' + this.parentNode.id )
        }
    }
}

<body onload="checkRadio()">
<div id="rBox">
<div class="rad" id="group-1">
      <input type="radio" name="option" />
      .... 
    </div>
    <div class="rad" id="group-2">
      <input type="radio" name="option" />
      ....     
    </div>
    ....
 </div>
</body>

use jquery
use jquery-> $.live()

If I understood you right, this may help you:

var num_of_options = 4;
var selected = new Array();
var op, i, j;

for (j=1;j<=num_of_options;j++) {
    op = document.getElementsByName("rd"+j);
    for (i in op) {
        if (op[i].checked) {
            selected[j] = op[i].value;
        }
    }
}

selected will contain the selected values for the radio groups in each divs.

Here is crude sample code that will show the selected value in each div:

var arrDivs = ["div1", "div2", "div3"];
for (var i = 0; i < arrDivs.length; i++) {
    var oDiv = document.getElementById(arrDivs[i]);
    if (oDiv) {
        var arrInputs = oDiv.getElementsByTagName("input");
        var blnFound = false;
        for (var j = 0; j < arrInputs.length; j++) {
            var oInput = arrInputs[j];
            if (oInput.type == "radio" && oInput.checked) {
                alert("The value of selected radio in div '" + arrDivs[i] + "' is: " + oInput.value);
                blnFound = true;
                break;
            }
        }
        if (!blnFound)
            alert("div '" + arrDivs[i] + "' contains no selected radio button");
    }
    else {
        alert("div was not found: " + arrDivs[i]);
    }
}
发布评论

评论列表(0)

  1. 暂无评论