I'm having troubles selecting a specific textarea element with an ID I know. Worst part of this is that the HTML page I have to work on contains two textarea with this ID. I know this is not right, but I have no control over that part.
So, in that page I have two main elements: <div id="left_column">
and <div id="right_column">
which both contains an element <textarea id="CPR_DESCRIPTION">
Between the left and right_columns div and their respective #CPR_DESCRIPTION
child textarea there is a plethora of other elements and they are similar on each side.
I wish to select both these #CPR_DESCRIPTION
textarea separately (because I have to add the content of the #left_column
's #CPR_DESCRIPTION
into #right_column
's one) but all my attempts are failing so far.
I have tried:
$('#left_column textarea#CPR_DESCRIPTION');
$('#left_column, textarea#CPR_DESCRIPTION');
$('#left_column').find('textarea#CPR_DESCRIPTION');
It's worth noting that I manage to retrieve #left_column
as a div, but when it comes to retrieving #CPR_DESCRIPTION
, no success. Also note that #CPR_DESCRIPTION
is NOT a direct child of #left_column
.
Here is the relevant part of HTML as asked (prepare your eyes for horrible stuff):
<div id="left_column">
<div id="property">
<div class="body>
<div class="inner">
<iframe id="property">
<html>
<body class="dialog">
<div id="G360Layout">
<form id="fieldsForm">
<div id="mainContainer">
<div id="container_2">
<div id="fields_container">
<div id="container_Description">
<div id="component_188">
<table>
<tbody>
<tr>
<td>
<textarea id="CPR_DESCRIPTION" name="CPR_DESCRIPTION">
egeezezez
</textarea>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</body>
</html>
</iframe>
</div>
</div>
</div>
</div>
So this is the monster I have to deal with, as you can see there are already duplicated IDs and worst part is that #right_column
is EXACTLY the same.
here is my jQuery:
function doOnLoadLayoutDescription() {
alert("inside doOnLoadLayoutDescription");
var leftColumn = $('#left_column');
//alert("leftColumn.length="+leftColumn.length);
//var description = $('textarea#CPR_DESCRIPTION');
//var description = leftColumn.find('textarea#CPR_DESCRIPTION');
var description = $('#left_column').find('textarea[id=CPR_DESCRIPTION]');
alert("description.length="+description.length);
if(description.val()) {
alert("inside description.val()");
var text = description.val();
alert(text);
var rightColumn = $('#right_column');
var readOnlyDescription = rightColumn .find('textarea#CPR_DESCRIPTION');
if (readOnlyDescription.length) {
alert("inside readOnlyDescription.length");
readOnlyDescription.val(text);
alert("switch done");
}
}
alert("ouside doOnLoadLayoutDescription");
}
NOTE: I changed the question a bit because I said it was a div but it was a textarea, even if doesn't change much, might be a good idea to rectify this now.
NOTE2: This HTML page is generated from a .jsp page that is part of a Case Management System called Case360 which is not so known. As all the content of the .jsp is generated through a drag-and-drop layout builder, it explains why this HTML is almost impossible to read comfortably.
EDIT I think I found the issue: The script I gave up there is actually being wrapped up inside the iframe element and thus only searches inside that frame, thus not finding my #left_column element. I have tried putting it outside of the iframe, to gain scope on the whole page, not only the frame but then my form's script which calls the other script won't find it now. I need to get a way to get out of that frame stuff in order to work properly. I am working on that right now.
Any idea guys?