2

HTML

<input type="checkbox" value="One" checked="checked" id="r1" name="g" /> <label for="r1"> One </label> <input type="checkbox" value="Two" id="r2" name="g" /> <label for="r2"> Two </label> <input type="button" value="Status" onclick="MyFunction()" /> 

Javascript

function MyFunction() { var chk = document.getElementsByName("g"); for (var i = 0; i < chk.length; i++) { if (chk[i].checked == true) { alert("Checkbox at index " + i + " is checked!"); alert("Text at index " + i + chk[i].nextSibling.innerHTML); } } } 

Here I am getting the Index of the checkboxes which are checked. How to get the text of the selected Checkboxes?

Demo:http://jsfiddle.net/u95uN/

Thanks for ur help.

7 Answers 7

2

Because in many browsers, the nextSibling will be an empty text node.

USE nextElementSibling instead of nextSibling

WORKING DEMO

The ChildNode.nextElementSibling read-only property returns the element immediately following the specified one in its parent's children list, or null if the specified element is the last one in the list.

function MyFunction() { var chk = document.getElementsByName("g"); for (var i = 0; i < chk.length; i++) { if (chk[i].checked === true) { alert("Checkbox at index " + i + " is checked!"); alert("Text at index " + i + chk[i].nextElementSibling.innerHTML); } } } MyFunction(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot!! btw what is difference between nextElementSibling.innerHTML and nextElementSibling.innerText?
@Johnny stackoverflow.com/questions/9727111/… .Make my answer as accepted
0

Since you tagged this with jQuery I'll answer using jQuery. Here's the fiddle: http://jsfiddle.net/5xL56/3/

Explanation: On button click:

$("#btn").click 

It will run through every checkbox with the name "g":

$("input[type=checkbox][name=g]").each 

Comments

0

Try this, You are writing JavaScript on load, mention in head or body as below:

 function MyFunction() { var chk = document.getElementsByName("g"); for (var i = 0; i < chk.length; i++) { if (chk[i].checked === true) { alert("Checkbox at index " + i + " is checked!"); alert("Text at index " + i + chk[i].nextElementSibling.innerText); } } } 

Demo

Comments

0

Use chk[i].value to fetch the value of the checked checkbox.

alert("Text at index " + i + chk[i].value); 

Demo

2 Comments

Yes, but i want the Text, not the value.
This solution will work as long as the value equals the text. Otherwise, use Samitha's solution. :)
0

Please correct this line !

alert("Text at index " + i + chk[i].nextSibling.nextSibling.innerText); 

Or you can use;

alert("Text at index " + i + chk[i].nextElementSibling.innerText); 

Or you can use; (My way)

alert("Text at index " + i + chk[i].value); 

To see this options, you can write in javascript your code "console.log(chk[i])" than you can see your options to get the text.

Comments

0

Try this I think this would work :

$.each($("input[name='g']:checked"),function(i,element){ var id = $(element).attr("id"); var text = $("label[for='"+id+"']").text(); alert(text); }); 

Comments

0

Use nextElementSibling instead of nextSibling

Fiddle demo

nextSibling checks for whatever there is following the current element and hence even line-break or space etc are also considered even if these are not HTML elements and nextSibling() is only for that.

But nextElementSibling searches for the next HTML element that here is the <label> and hence it is what you need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.