Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find element and set the value

Tags:

jquery

i have a div element with id hover-1, within the div i have a hidden form element having class name text_page_title in the hierarchy. i got the parent div element id dyanamically in a variable, now i want to find the hidden element having class name text_page_title and set its value to 'foo'

structure is like:

<div id="hover-1">
<input type="hidden" class="text_page_title">
</div>
<div id="hover-2">
<input type="hidden" class="text_page_title">
</div>

I m trying to do it like:

$($parentId).find('input.text_page_title').val('foo');

but it doesnt work, m i missing something?

like image 567
Firdous Avatar asked Feb 23 '23 01:02

Firdous


2 Answers

This works:

$("#hover-1").find('input.text_page_title').val('foo'); // or $("#hover-2")

If you want your parent id in a variable:

var parentId = $("#hover-1"); // or $("#hover-2");
parentId.find('input.text_page_title').val('foo');

working jsFiddle

like image 86
Benjamin Crouzier Avatar answered Mar 08 '23 12:03

Benjamin Crouzier


Assuming that parentId is the variable name containing the id string ("hover-1"), you should rewrite your quesry like this:

$('#'+parentId).find('input.text_page_title').val('foo');
like image 22
MeLight Avatar answered Mar 08 '23 12:03

MeLight