-3

I am completely a beginner in java script. As i know, by using this line of code:

document.getElementById('ID').value

It looks in the entire PHP file to find the value of that id (it's a text input). Is there any alternative for the documents on that line of code that let me to search only in a class?

for example i have a class called number, so :

this.block = $("#number");

can i use something like that:

this.block.getElementById('ID').value;

Or there is any another line of code that give me the value of a text input? Thanks a lot

user9217262
  • 35
  • 1
  • 1
  • 8
  • If `document.getElementById("ID")` is not the element that you expect, then your HTML is invalid, and you have to fix that first. – Sebastian Simon Apr 24 '18 at 21:58
  • 1
    `i have a class called number` No, the selector string there, `#number`, refers to an element with an ID of `number`. Classes are selected with a dot prefix, eg `.custom-anchor`. Tag names (such as `span`) are selected without any prefix at all, such as `'span'`. Look them up here: https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Selectors – CertainPerformance Apr 24 '18 at 21:59
  • Possible duplicate of [Why does getElementById not work on elements inside the document element?](https://stackoverflow.com/questions/16475636/why-does-getelementbyid-not-work-on-elements-inside-the-document-element) – Sebastian Simon Apr 24 '18 at 21:59
  • It works perfectly for me. But as i said it apparently search into the entire PHP, i think if it search in only a class, it would be faster – user9217262 Apr 24 '18 at 22:00
  • @user9217262 Not really. Since IDs are supposed to be unique, their lookup is often optimized. – Sebastian Simon Apr 24 '18 at 22:01
  • `It looks in the entire PHP file` - to be a tad more precise: it searches the DOM tree that the browser has built by parsing your HTML. And lookup by #id is very fast because ids must be unique within a page. – Peter B Apr 24 '18 at 22:01

1 Answers1

0

You can use

$("#number").find("#id")

to search descendants of #number.

But if your class is number you should use $(".number") instead since that is the class selector.

Daniel
  • 3,312
  • 1
  • 14
  • 31