0

What is Javascript alternative for this:

$('#clientDetailModal #heightValue')

I need it because in my code this works:

document.getElementById('heightValue').checkValidity()

but this doesn't:

$('#clientDetailModal #heightValue').checkValidity()

And I need to select only heightValue within clientDetailModal div.

4
  • You should not use duplicate ID inside same document. Use a class and try a class selector. With that being said, you should probably tell us what checkValidity function is. Seems like it is exposed on DOM elements and not on jQuery object. Commented Jun 4, 2013 at 15:57
  • The ID identifier must be unique (only 1 per page), so if you do this: $('#clientDetailModal #heightValue') in jquery it's the same as doing this: $('#heightValue') but slower. Commented Jun 4, 2013 at 15:58
  • @tborychowski I don't know if OP edited (didn't see it at first) but question states "I need to select only heightValue within clientDetailModal div". Commented Jun 4, 2013 at 16:00
  • @dystroy Aah, I see it. Thanks. Still - it's a bad practice and classes should be used instead. Commented Jun 4, 2013 at 16:03

5 Answers 5

5

Try $('#clientDetailModal #heightValue')[0].checkValidity()

The reason you need to do the [0] is, (as per the jquery id selector documentation)

Calling jQuery() (or $()) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element

Since you'll get a collection with 1 DOM element (assuming you don't have multiple ids), you need to then explicitly "select" that element using the [0].

Sign up to request clarification or add additional context in comments.

Comments

2

You could use get to get the DOM element :

$('#clientDetailModal #heightValue').get(0).checkValidity()

Just to be sure, as your question might be a little ambiguous : only one element can have a given ID in HTML. So if your element is either absent or inside #clientDetailModal, then you could as well use

$('#heightValue').get(0).checkValidity()

It would also be faster. But in that case, there would be nothing wrong in using document.getElementById.

Comments

1

Since document.getElementById('heightValue').checkValidity() works, it means your function checkValidity() is attached on native DOM elements. This means, you can do:

$('#clientDetailModal #heightValue')[0].checkValidity() 

Plus: If your HTML is valid with no duplicate IDs, you can simply do

$('#heightValue')[0].checkValidity() 

Comments

0

Since the OP asked for a JavaScript alternative. On modern browsers,

document.querySelector ('#clientDetailModal #heightValue')

will return the element you are asking for.

The direct equivalent would be

document.querySelectorAll ('#clientDetailModal #heightValue')

which returns an array of elements matching the selector requested, do yrou will need to add the [0] as per the other answers.

Comments

-1

I presume this is what you're looking for :

document.getElementById('clientDetailModal').getElementById('heightValue').checkValidity();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.