morizvonlanga morizvonlanga - 1 year ago 95
HTML Question

jQuery find element with custom attribute 1 and 2

I've written this code to load dynamic content:

$('.item').click(function(){
var path = $(this).attr('path')
});


and this to create anchor links when the content is loaded:

var title = $(this).attr('title');
window.location.hash = title ;


And this part to check which content was loaded:

var title = $('.item').attr('title');
if (window.location.href.indexOf(title) > -1) {
console.log("found it");
}


No I would like to load the content via the anchor link so I need to get a element with the title who fits to the path. The HTML markup looks like this:

<div class="item" path="/mainfolder/subfolder/item/" title="I'am the title">


Do you have any ideas to do something like that? I just need to know how I'am able to search for elements with:
attribute 1 which has attribute 2


Thanks for answers guys!

Answer Source

This is how to select it ^^

if($('[path="/mainfolder/subfolder/item/"]').length > 0){
  console.log("Found1!");
};
if($('[title="I\'am the title"]').length > 0){
  console.log("Found2!");
};

if($('[path="/mainfolder/subfolder/item/"][title="I\'am the title"]').length > 0){
  console.log("Found3!");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input class="item" path="/mainfolder/subfolder/item/" title="I'am the title"/>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download