0

I have a table with id and a class. I am using a plugin to fix the first two column and the header in order to meet my requirements. My table is defined by the tag:

<table class="fancyTable" id="myTable03" cellpadding="0" cellspacing="0">

I wanted to add another features to this table. After inspecting the DOM through firebug, I found out that the table is broken up into two parts by the plugin(I supposed). While one part of the table becomes

<table class="fht-table fancyTable" style="margin-top: 0px;">

and the other one becomes

<table id="myTable03" class="fancyTable fht-table fht-table-init" cellspacing="0" cellpadding="0" style="width: 926px; margin-top: -40px;">

I want to add attribute id to the table containing the class "fht-table fancyTable". How can i do that? I'm using Jquery/javascript.

putvande
  • 15,068
  • 3
  • 34
  • 50
L4reds
  • 412
  • 2
  • 5
  • 21
  • Please post what you have tried so far. The jQuery documentation has a section about DOM manipulation, you might want to have a look at it first: http://api.jquery.com/category/manipulation/general-attributes/. If you don't know how to select the correct element have a look at [jQuery multiple class selector](http://stackoverflow.com/q/1041344/218196). – Felix Kling Aug 12 '13 at 09:31

3 Answers3

2

You'll need something like this:

$('.fht-table.fancyTable').not('#myTable03').prop('id', 'yourId');

or

$('.fht-table.fancyTable').not('.fht-table-init').prop('id', 'yourId');
billyonecan
  • 20,090
  • 8
  • 42
  • 64
1

From the second one you have already an id myTable03

If you want to add attribute id to the table containing the class fht-table fancyTable

Then try this,

$(".fht-table.fancyTable:not('#myTable03')").attr('id','myTable04');

Read How can I select an element with multiple classes?

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • you definitely show me the hint. $(".fht-table.fancyTable").attr("id", "myTable"); $(".fht-table.fancyTable.fht-table-init").attr("id", "myTable03"); i got my answers now. – L4reds Aug 12 '13 at 09:44
0

Attribute ID can be added as normal attribution for properties. But reference to that element should not be get by Id.

You can use:

$("div").attr("id", "div1");

Note: Two element must not have same id.

var divs = document.getElementsByTagName('div');

or

var divs = document.getElementsByClassName('fancyTable');


for(var i = 0; i<divs.length;i++) {
    divs[i].setAttribute("id","div"+i);
}
Ashwani
  • 3,463
  • 1
  • 24
  • 31