---
title: "Why does Jquery only affect the first div element?"  
description: "Why does Jquery only affect the first div element?"  
author: "Anonymous User"  
published: 2013-06-06  
updated: 2018-05-21  
canonical: https://www.mindstick.com/forum/1005/why-does-jquery-only-affect-the-first-div-element  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# Why does Jquery only affect the first div element?

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances), \
I am using the "**[replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy)**" [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) all **non-numeric** [values](https://www.mindstick.com/forum/327/sum-textbox-values) in a div.\
It seems **Jquery** replace only affects the first element.\
Here is my **Jquery**:\
**$('#comment').each(function() {** **[var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) thz = $(this);** **var repl = thz.[html](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript)(thz.html().replace(/\D+/g, ''));****});**\
HTML [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):\
**<a id="comment1" href="#"> c2fđf011. </a>****<a id="comment1" href="#"> c20ff113. </a>****<a id="comment1" href="#"> c201gf76341. </a>**\
[Result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency):\
**2011 c20ff113. c201gf76341.**\
The result I want is:\
**2011 20113 20176341****\**Please help,Thanks in advance. \

## Replies

### Reply by Ella Scott

Post is removed by the Admin.

### Reply by AVADHESH PATEL

Hi Expert,\
You have duplicate ids, Which is invalid and also jquery ID selector will return only the first one that appears in DOM. Change it to class and see it working:\

```
$('.comment').each(function() {      var thz =  $(this); var repl =     thz.html(thz.html().replace(/\D+/g, '')); });
```

**\****HTML**\

```
<a class="comment1" href="#"> c2fdf011. </a> <a class="comment1" href="#">c20ff113. </a> <a class="comment1" href="#"> c201gf76341. </a>
```

By the way had your id been like this:-\

```
<a id="comment1" href="#"> c2fdf011. </a> <a id="comment2" href="#">c20ff113. </a> <a id="comment3" href="#"> c201gf76341. </a>
```

Starts with selector will help you.\

```
$('[id^=comment]').each(function() { // While using this better give a container context $('[id^=comment]', 'container').each(function...    var thz = $(this);    var repl = thz.html(thz.html().replace(/\D+/g, ''));});
```

\


---

Original Source: https://www.mindstick.com/forum/1005/why-does-jquery-only-affect-the-first-div-element

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
