views:

100

answers:

5

Hi there, i have three div's on a page with same ID's

<div id="downloaded">1200</div>

Now when i call the function downloadThis("filename","divID"); i want that the value of every div with id "Downloaded" should change, i am using jquery. Kindly help.

A: 
function downloadThis(a, b) {
    $("#downloaded").text(b);
}

But as James Black mentioned, your ID elements should all be unique on a page. I would change it to be a class name like so:

<div class="downloaded">...</div>

function downloadThis(a, b) {
    $(".downloaded").text(b);
}
jessegavin
A: 

If I'm not mistaken, Id's need to be unique. But what you want to achieve can be done by using the class attribute

$('.downloaded').toggleClass('.done')
Vijay
+4  A: 
Darin Dimitrov
.text() function works too (in this case)
jessegavin
Using IDs doesn't even work, as jQuery selects only the first element, assuming that there will be no more as IDs should be unique. So `$('#downloaded').html('new value')` only affects the first div only - using classes is the way to go.
Tatu Ulmanen
@Tatu, you're correct, I've updated my answer.
Darin Dimitrov
A: 

I would change the id to be unique, and perhaps you can put in either a common className or have downloaded_1, downloaded_2, downloaded_3 as the ids.

If you go with the latter solution then you can get the elements by:

var elems = $('div').filter(function() {
    return this.id.match(/downloaded/)})

Then you can use the html function to set them, or do whatever else you want.

James Black
A: 

Ok i have assign different values to the DIV for example.

1500

Now this div is on 2 sides left and center. Infront of both is a download button clicking which following code is executed,

function downloadIt(file,divName){ var i = parseInt($("#"+divName).text()); var updated = i+1; //alert('#'+divName+''); $("#"+divName).html(""+(updated)).hide().fadeIn("fast"); setTimeout(function(){ $("#downloadBar").slideDown("fast") }, 700); setTimeout('tryToDownload("new.php?fileName='+file+'")', 400); setTimeout(function(){ $("#downloadBar").slideUp("fast") }, 5000);

}

But still it doesn't updates the DIV's only 1 div on the left side is updated!!

Ahsan