user3314032
user3314032

Reputation: 179

Cannot get Text from label jQuery, possible ID error?

If i use another ID to get some data, it's no problem, but when i use "lbl_BlendCalc_1.1_I" as an ID i cannot get the text. (Changed this ID is not possible).

<input id="yes1" type="checkbox">
<label style="width: 100px;" id="lbl_BlendCalc_1.1_I">test</label>

$('input').change(function() {
    if (this.checked) {
        alert($("#lbl_BlendCalc_1.1_I").text()) ;
    }
});

http://jsfiddle.net/wycvy/338/

I believe it is the ID that is doing something, but what i do not know.

Upvotes: 0

Views: 73

Answers (3)

sonam gupta
sonam gupta

Reputation: 785

The character dot(.) is breaking the id. so if you dont want to change the id then escape it using backslashes '\\'.

Upvotes: 2

Sudharsan S
Sudharsan S

Reputation: 15393

Id be broken if you using special characters like @!. So we need to escaping the special characters with two backslashes(\\).

$('input').change(function() {
  if (this.checked) {
        alert($("#lbl_BlendCalc_1\\.1_I").text()) ;
  }
});

Fiddle

Upvotes: 1

Adil
Adil

Reputation: 148120

You can use attribute selector, giving id in quotes will escape the dot . that is reserved for class selector for jQuery.

Live Demo

alert($("[id='lbl_BlendCalc_1.1_I']").text()) ;

Upvotes: 1

Related Questions