简体   繁体   中英

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?

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

document.getElementById(id_In_Restaurant)

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

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

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM