Shafizadeh
Shafizadeh

Reputation: 10340

How to append ajax response in the unknown element?

I have several <div> without class name. something like this: (* is where I want to append ajax response)

<div>
<div> text 1</div>
<div> text 2 </div>
<div> * <h1></h1> </div>
<div> text 3 </div>
</div>

The only clue that I have is <h1>. because the number of <div> is not constant (sometimes I have just 1 div). In other word, How can I select div that is containing <h1> and then append ajax response at the beginning of that ?

Upvotes: 1

Views: 201

Answers (2)

AleshaOleg
AleshaOleg

Reputation: 2201

To choose this h1 from div you can simply use this jQuery code:

var h1 = $("div > h1")

or

var h1 = $("div h1")

Upvotes: 0

dotnetom
dotnetom

Reputation: 24901

You can use jQuery has selector. In this cae you can find your element like this:

var yourElement = $('div').has('h1');

If you want to add some data that you receive from AJAX request, you can use method prepend:

var resultFromAjax = 'some result';
var yourElement = $('div').has('h1');
yourElement.prepend(resultFromAjax);

Upvotes: 1

Related Questions