-1

This is my Html Snippet

<div class="style" title="Demo.1"></div>
<div class="style" title="Sample"></div>

I am trying to get an html element using the selector

$('[title=Demo.1]').html()

and i got this error message

jquery-1.8.3.js:4680 Uncaught Error: Syntax error, unrecognized expression: [title=Demo.1]

but it is working for $('[title=Sample]').html()

I think it is because of the decimal point "." Is there any other way to solve this

Prem
  • 630
  • 2
  • 11
  • 25

2 Answers2

3

You need quotes on the value:

$('[title="Demo.1"]').html()

Example:

console.log($('[title="Demo.1"]').html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="style" title="Demo.1">Demo1</div>
<div class="style" title="Sample">Sample</div>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The Attribute Equals selector use valid identifers or quoted strings to target the appropriate element by its value, however quoted strings are generally more reliable when targeting non-identifier attributes like this one.

// This will target any elements that have a title attribute that is exactly "Demo.1"
$('[title="Demo.1"]').html();
Rion Williams
  • 74,820
  • 37
  • 200
  • 327