0

Hi I am trying to get the HTML of INPUT tag. But unable to..

 <input type="checkbox" name="_QS4_CNA" id="_Q0_C7" class="mrMultiple" value="NA">      
  <label for="_Q0_C7">
    <span class="mrMultipleText" style="">None of these</span>
  </label>
 </input> 

And I am trying access as

 var dat=$(':checkbox#_Q0_C7').html();
 alert(dat);

But i cannot access this. Please help me on this..

2 Answers 2

5

The ".html()" method gets the contents of an element, and not the element itself. In your case, the problem is that your HTML is invalid. An <input> tag cannot have content. As far as the browser is concerned, the tag ends where the <label> tag starts, and the browser just ignores the closing tag.

Note that when you've got an "id" attribute to use to find an element, you don't need any other qualifiers in the selector (like ":checkbox"). Just "#_Q0_C7" is all you need, because "id" values have to be unique anyway.

edit — Note that if all you want is to get some attribute (like the value or the "checked" status) from the element, you can certainly do that:

var $cb = $('#_Q0_C7');
var isChecked = !!$cb.prop('checked'); // force a real boolean value
var value = $cb.val();
Sign up to request clarification or add additional context in comments.

1 Comment

That may be true, but there's nothing in the question as posed that would suggest that. I'll add that to the answer however.
1

You can try accessing the RAW underlying DOM element and use its innerHTML property.

 var dat = $(":checkbox#_Q0_C7")[0].innerHTML;

But like mentioned by Pointy, that might still get you nothing. Not sure what (if any) input elements have actual siblings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.