开发者

how to change all div values on a page

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.


It is not valid to have more than one element on the page with the same ID.

To set value you could use the html function:

$('#downloaded').html('new value');>

A more semantically correct way to achieve this is to assign the divs a class name and use a class selector:

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

$('.downloaded').html('new value');


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);
}


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.


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')


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

<div id="song1Downloads">1500</div>

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("<b><u>"+(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!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜