1

I'm using this line of code to try and scroll to an element by its title:

  $("#wrapper").animate({scrollTop: $(".placementName[title='" + placementName + "']").offset().top - 10}, "slow");

but the problem is i have few objects in my HTML code with the same title and same class placementName, how can i ad an id to the rule? the id is somewebsite.com so i tried something like:

$("#wrapper").animate({scrollTop:$("#somewebsite.com.placementName[title='" + placementName + "']").offset().top - 10}, "slow");

but it didn't work...any idea? thx

yariv bar
  • 936
  • 3
  • 20
  • 39

4 Answers4

1

You can just do :

  $("#wrapper").animate({scrollTop:$("#somewebsite\\.com").offset().top - 10}, "slow");

Since the id should be unique and is all you need to specify the element.
The dot in the id can be problematic, but escaping the dot should work.

See also: jQuery dot in ID selector?

Community
  • 1
  • 1
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
1

The selector you want is

$("#somewebsite\\.com.placementName[title='" + placementName + "']")

That is because you need to escape . jquery reads $("#somewebsite.com.placementName") as id: #somewebsite and classes: .com .placementName

Someone wrote here that somewebsite.com is not valid id. That is not true. More here https://stackoverflow.com/a/79022/2672983

If id somewebsite.com is unique, just use

$("#somewebsite\\.com")
Community
  • 1
  • 1
Krisa
  • 85
  • 5
0

If you have dots in your id, you can access checking the attribute, like this:

$("#wrapper").animate({scrollTop:$("[id='somewebsite.com'].placementName[title='" + placementName + "']").offset().top - 10}, "slow");

Anyway, since ids must be unique, checking the id should be enough:

$("#wrapper").animate({scrollTop:$("[id='somewebsite.com']").offset().top - 10}, "slow");
frikinside
  • 1,244
  • 1
  • 9
  • 19
-1

somewebsite.com is not a valid id. Please change it and then it should work. When you do this :$("#somewebsite.com.placementName[title='" + placementName + "']") then jquery looks for a dom with id somewebsite and classes com & placementName

Harry Bomrah
  • 1,658
  • 1
  • 11
  • 14
  • IDs can have a period (dot) in them but they are problematic – StudioTime Jan 12 '16 at 11:44
  • @DarrenSweeney yes but if u use them directly in jquery selector. the selector interpret as a class after ther dot. so you can not directly use it for selection. I didnt know that OP cant change the `id` as the easiest way is to change the id if possible. – Harry Bomrah Jan 12 '16 at 11:47
  • Yes you can, as my comment says, you escape the period e.g. `#unique\.id` – StudioTime Jan 12 '16 at 11:49
  • I never said you posted a wrong answer. sure we can escape. – Harry Bomrah Jan 12 '16 at 11:53