ameliapond
ameliapond

Reputation: 218

How to change parent element properties by his child using javascript?

I would like to know how to access a parent element when it doesn't have any identifier. Typically I want to do the following:

<td>
     <a title="mySweetTitle"/>
</td>

Access the "td" using his "a" child to modify his properties.

Upvotes: 1

Views: 5936

Answers (3)

MKAD
MKAD

Reputation: 457

what you are looking for ist $(this).parent() look at my example i hope it helps

$(document).ready(function() {
  $('.test').on('click', function() {
    $(this).parent().append("<button>test2</button>");
  });
});
<!doctype HTML>
<html>
	<head>
	<title>Test Umgebung</title>
	<meta charset="UTF-8">
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

	</head>
	
	<body>
		<div>
			<button class="test">test</button>
		</div>
	</body>
</html>

Upvotes: 1

Dmitry Volokh
Dmitry Volokh

Reputation: 1646

you should use parentElement property https://developer.mozilla.org/en/docs/Web/API/Node/parentElement

example:

document.getElementById('your_id').parentElement

in your case you can use

document.getElementsByTagName('a')[0].parentElement

Upvotes: 1

leguano
leguano

Reputation: 171

Maybe this could help you:

$("a").bind("click" , function(){
    var parent = $(this).parent();
});

Upvotes: 1

Related Questions