1

I would like people to view the latest reviews on my website and when they click on it,

it should bring them to the business page where the review is written but the page should also scroll down to where the review is.

I'm thinking that it might be possible if I give a different class name to each of my divs with the review ID in the class name? (I need to give them the same id to apply format with css).

More specifically, how do I do with jquery to go straight to the DOM review_XXX (xxx for the review ID)

Nicolas de Fontenay
  • 2,170
  • 5
  • 26
  • 51

4 Answers4

3

You don't need jQuery for this. Simply give the links a href with a # prefix followed by the ID of the element to want to jump to, e.g.:

<a href="#foo">Foo</a>
...
<div id="foo">This is the div to jump to</div>

See "anchors with the ID attribute" in the W3C HTML Specification.

Interesting excerpt from the above website:

Use id or name? Authors should consider the following issues when deciding whether to use id or name for an anchor name:

  • The id attribute can act as more than just an anchor name (e.g., style sheet selector, processing
    identifier, etc.).
  • Some older user agents don't support anchors created with the id attribute.
  • The name attribute allows richer anchor names (with entities).

Also see (for HTML5):

Community
  • 1
  • 1
karim79
  • 339,989
  • 67
  • 413
  • 406
  • the thing is I use the same id for every review on the same page right now so each review is wrapped in a div with id "review". I think I have to use what has been mentioned below: The name attribute. – Nicolas de Fontenay Apr 04 '11 at 14:25
  • @ndefontenay - Duplicate IDs are invalid in HTML documents. You will not get a simple HTML solution like the one I posted working nor will you be able to use JavaScript/jQuery. First address the dupe IDs. Check this: http://www.w3.org/TR/html40/struct/global.html#h-7.5.2 – karim79 Apr 04 '11 at 14:29
1

I would use the name attribute of the anchor tag and add the value of the name attribute to the url.

So somewhere on your page your review title would be something like

<h2 name="current_item">...</h2>

And your url would look like:

http://somehost.com/somepage/structure.html#current_item
Jonas Geiregat
  • 5,214
  • 4
  • 41
  • 60
0

Assuming your review "ID" values are really unique identifiers (otherwise they probably shouldn't be called "ID" values), you can give them to the relevant elements as "id" values:

<div id='review_42097'>
  that movie sucked
</div>

Then you can just put "#review_42097" at the end of your URL!

<a href='http://my.reviews.com/latest/reviews.php#review_42097'>Click to read reviews!</a>
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I use mod_rewrite for my website so I will have a url that looks like this: http://my.reviews.com/en/region/country/city/business/business_name/ Can I add #review_42097 at the end? http://my.reviews.com/en/region/country/city/business/business_name/#review_42097 – Nicolas de Fontenay Apr 04 '11 at 14:21
0

Are these IDs a part of a RESTful URL scheme? For instance:

http://yoursite.com/reviews/123456

Most likely you don't need jQuery to just generate a link for this.

If you aren't just linking to the page, you can store the id in a data attribute on the element and access it to do your logic:

<div id="myElement" data-review-id="123456"></div>

var id = $('#myElement').data('review-id');

// do other logic
Eli
  • 17,397
  • 4
  • 36
  • 49