Get text written in Anchor tag
Solution 1:
If you are not using jQuery, then below line would help.
document.getElementById('a1').innerHTML
Solution 2:
First you have to write a close this tag
<input name="CheckBox" id="c1" type="checkbox" CHECKED="" value="on"/>
<a class="a_c" id="a1"> Something </a>
Anchor has id assigned yet therefore you can get access directly:
$("#a1").text()
If anchor did not have id and will be always after checkbox ( can be separated by other tags)
$('#c1').next("a").text();
otherwise if will be checkbox's sibling and in this branch is one anchor ( not necessarily just after checkbox)
$('#c1').parent().find("a").text();
Solution 3:
$('#c1').next("a").text();
Solution 4:
I'm adding a pure Javascript answer that doesn't rely on .innerHTML
, which can be much slower than proper DOM level accessors. This also assumes you are retrieving HTML free content.
Using the original question with a completed a tag:
<td class="td1">
<input name="CheckBox" id="c1" type="checkbox" CHECKED="" value="on"/>
<a class="a_c" id="a1">Something wonderful</a>
</td>
Javascript using the id from the anchor tag:
document.getElementById("a1").textContent;
If you need to support Internet Explorer 8 or lower, you'll have to either selectively use .innerText
for IE 6-8, or stick with .innerHTML
. .innerText
was not supported by Firefox for a while, so it shouldn't be used for anything but IE 6-8.
Solution 5:
If the anchor tag has an ID as in your example, then $('#a1').text();
should do the trick.