Answer the question
In order to leave comments, you need to log in
How to access DOM elements of the document tree from a function?
Friends, I'm completely dumb, it seems, I can't figure out how to select elements from a function that is connected by a file. It seems like a simple task and everyone does it, I can’t do it, what’s wrong?
Here is an example document
here is a document
<!DOCTYPE html>
<html lang="ru">
<head>
<title>Заголовок</title>
</head>
<body>
<div class="wrapper">
<h1>Заголовок</h1>
<a href="#" class="order-btn">кнопка</a>
</div>
<script>
$(document).on('click','.order-btn',function(){
addParam();
});
</script>
<script type="text/javascript" src="/main.js"></script>
</body>
</html>
function addParam() {
var datatitle = $(this).parent().find('h1').text();
alert(datatitle);
}
Answer the question
In order to leave comments, you need to log in
$('.order-btn').on('click', function(){
// code here
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question