user14955679
user14955679

Reputation: 181

jquery selecting div id within div id

Through jquery I am trying to target a div with an id within a div container with a different id. Example:

<div id="container-type-a">
<div id="inner_number_2201"></div> //<<<< this id is a passed dynamic ID and changes. Its passed into the function. 
</div>

So I would want to do something like document.getElementById('> container-type-a' > 'inner_number_' + id + ) but this is not the right syntax. Can something like this be done?

Upvotes: 1

Views: 972

Answers (4)

Afsar
Afsar

Reputation: 3124

You can use below code if your dynamic div is direct decedent.

$('#'+$('#container-type-a >div').attributes.id.value)

Upvotes: 1

Krishna Devashish
Krishna Devashish

Reputation: 86

Since Id is unique throughout the document so you can use the following syntax

$('#inner_number_' + unique_dynamic_id)

This will return you the jquery object of the element.

Upvotes: 1

epascarello
epascarello

Reputation: 207531

If you want to use a selector, you would use querySelector

var inner = document.querySelector("#container-type-a #inner_number_" + id_In_Restaurant);

But since ids are supposed to be unique, it makes no sense to use two ids. Just been to use getElementById

var inner = document.getElementById("inner_number_" + id_In_Restaurant);

Getting a reference to the parent element

var inner = document.getElementById("inner_number_" + id_In_Restaurant);
var innersParent = inner.parentElement;

Upvotes: 1

Snow
Snow

Reputation: 4097

ID should be unique in the HTML, so unless your HTML is malformed, you should be able to just

document.getElementById(id_In_Restaurant)

Upvotes: 1

Related Questions