0

This is my js code:

function myfunction(){
    $("#label_startDate[0]")[0].outerHTML;
}

and this is the html part:

<label id="label_startDate[0]" class="" style="">
    <span id="star_startDate[0]" style="display: none; color: red">*</span>
    <input name="startDate[0]" id="startDate[0]" value="2015-06-02" readonly="" class="forDatePicker hasDatepicker" size="14" onchange="changeEndDate(0); calculateDaysDuration('@[0]', 0); "type="text">
</label>

For the reason that I have a weird format of the id from the label, I cannot get the outerHTML. What should I do ? I cannot change the id label_starDate[0].

1
  • 1
    Consider using document.getElementById(...) instead. That's effectively what you're using anyway (and it will be hugely faster than the $ selector). Commented Jun 2, 2015 at 11:41

1 Answer 1

6

Use \\ to escape [] and also use .prop() to access properties

 $("#label_startDate\\[0\\]").prop("outerHTML");

From Docs

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

$(function() {
    alert( $("#label_startDate\\[0\\]").prop("outerHTML"))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label id="label_startDate[0]" class="" style="">
                        <span id="star_startDate[0]" style="display: none; color: red">*</span>
                        <input name="startDate[0]" id="startDate[0]" value="2015-06-02" readonly="" class="forDatePicker hasDatepicker" size="14" onchange="changeEndDate(0); calculateDaysDuration('@[0]', 0); "type="text">  
</label>

Sign up to request clarification or add additional context in comments.

1 Comment

Thx a lot great solution and well explained

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.