0

I use this code to get value from an input box:

var suggest_type = document.getElementById('ac-type').value;

Now I need to apply my js code on several other pages. I heard that it's not nice to repeat an ID on one website. So, I'm thinking to change to use class like this:

var suggest_type = document.getElementByClass('ac-type').value;

This doesn't get the value. How can I use class to get value?

Jenny
  • 1,729
  • 4
  • 19
  • 37
  • 1
    duplicate of this - http://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript – Mukesh Soni Jul 20 '12 at 13:48
  • 1
    `getElementByClass` does not exist. – Felix Kling Jul 20 '12 at 13:51
  • `getElementByClass` doesn't exist and the idea is semantically wrong, as there can be more elements with the same class. – Imp Jul 20 '12 at 13:55
  • Just to reiterate my answer which has received unecessary downvotes (unexplained I might add). ID's do not need to be unique across an antire website, only within a single page. – Jon Taylor Jul 20 '12 at 13:59

5 Answers5

1

Its absolutely fine to repeat ids across a website just not on a single html document.

ID's should be unique within one html page.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • 4
    care to elaborate with the downvotes? Or have you not bothered to read my answer properly? – Jon Taylor Jul 20 '12 at 13:50
  • I upvoted yours, but received three downvotes on mine, probably for not using the non-existent getElementByClass function ??? – adeneo Jul 20 '12 at 14:01
  • Maybe down vote makes some peeps feeling good of having a little power over others. – Jenny Jul 20 '12 at 14:05
1

It's actually :

var suggest_type = document.getElementsByClassName('ac-type')[0].value;

But I agree with Jon Taylor, ID's can be the same within a website, as long they are not duplicated on the same page.

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

You should stick with using ID's - they only have to be unique within a page.

If you must use classes, you need to use getElementsByClassName():

var elems = document.getElementsByClassName('ac-type');
var value = elems[0].value;

However this function is not well supported on older browsers, which is another good reason to stick with IDs.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • I am confused as to how this is different to my answer which has receieved 2 downvotes? :/ – Jon Taylor Jul 20 '12 at 13:53
  • It's not that different, except I showed how you _could_ do it with classes if you really had to. I did upvote you, BTW. – Alnitak Jul 20 '12 at 13:53
  • While I agree, my point is completely valid and makes the need for selecting by class unecessary. – Jon Taylor Jul 20 '12 at 13:55
  • Thanks for the answers. I didn't know same ID on different pages of one website is valid. I will use ID. – Jenny Jul 20 '12 at 14:04
0

try using this:

var elements = document.getElementsByClassName('ac-type'); // this is an array containing all matched elements
haynar
  • 5,961
  • 7
  • 33
  • 53
  • note that instead of `getElementByClass`, I've used `getElementsByClassName` – haynar Jul 20 '12 at 13:51
  • 1
    Not only is that `getElementsByClassName`, note the *Name*, but it also doesn't work in older IEs. – Imp Jul 20 '12 at 13:53
-1

Use jquery just by using -

$('.ac-type').val();

you will get value.

Sandip Karanjekar
  • 850
  • 1
  • 6
  • 23
  • 2
    Why jQuery and not Prototype.js or Mootools or Dojo or YUI or ... ? (this is a rhetorical question... if the question is not about a certain library, don't suggest to use one, unless it is really really necessary or you provide an alternative, library-free solution as well). – Felix Kling Jul 20 '12 at 13:53