0

I can't figure out how to escape this particular '. I have the following code:

//Insert Home Room Teacher
$j("tr:contains('Home Room')").after('<tr><td class="bold">Home Room Teacher</td><td><input type="text" name="[01]home_room_teacher" value="" size="25"></td></tr>');

[01]home_room_teacher is a field that contains the student's home room teacher's last name. One of our teachers last name is O'Shea. As is, the code works for every other teacher fine. If I load a student who has him for their home room, the ' in his last name breaks the script and none of it loads (there are multiple things in the page fragment).

If I do name="\[01]home_room_teacher\", the page loads properly but the field is blank. I've also tried to edit the actual value in the field to show O\'Shea, but that doesn't work either.

Please help.

EDIT: Here is the full code that is being run:

<script>
$j(document).ready(function() {

//Insert Counselor
$j("tr:contains('Family rep')").before('<tr><td class="bold">Counselor</td><td><input type="text" name="[01]Counselor" value="" size="25"></td></tr>');
//Insert Home Room Teacher
$j("tr:contains('Home Room')").after('<tr><td class="bold">Home Room Teacher</td><td><input type="text" name="[01]home_room_teacher\" value="" size="25"></td></tr>');
//Insert Teacher Rec
$j("tr:contains('School Entry Grade Level')").after('<tr><td class="bold">Teacher Recs Visible</td><td><input type="checkbox" name="[01]RecDisplay" value="1"></td></tr>');

//Hide Rows
$j("tr:contains('Lunch'), tr:contains('District'), tr:contains('Current'), tr:contains('Phone'), tr:contains('Student'), tr:contains('Track')").css("display", "none");

//Swap TR with TR
$j("tr:contains('Locker Combination')").insertAfter($j("tr:contains('Locker Number')"));

//Alternate Color Rows
$j("td:even").css("background-color", "transparent");
$j("td:odd").css("background-color", "transparent");
$j("tr:even").css("background-color", "#D7EBF4");
$j("tr:odd").css("background-color", "#ffffff");
});
</script>
Dfoles
  • 1
  • 2
  • you could use a special character like % in place of the ' like O%Shea and find and replace it in the html after your .after. Probably not the best solution though – StealthSpoder Feb 19 '16 at 19:29

1 Answers1

0

Try using the following javascript, this should work.

$(document).ready(function() {
  teacher = "O'Shea";
  $("#show").html('<tr><td class="bold">Home Room Teacher</td><td><input type="text" name=' + teacher + ' value=' + teacher + ' size="25"></td></tr>');
  $('#print').text($('#show').html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="show">

</table>
<div id="print">
</div>

And you can directly replace the variable teacher with : [01]home_room_teacher

stark
  • 2,246
  • 2
  • 23
  • 35
  • Unfortunately teacher pulls from a field within the student table, so I can't hard code O'Shea as that would not display properly for students that don't have him for home room. – Dfoles Feb 19 '16 at 20:07
  • @Dfoles : You don't need to hard-code O'Shea, I meant you can directly replace the variable `teacher` with the your `[01]home_room_teacher` variable. – stark Feb 19 '16 at 20:09
  • I tried that, as well as putting it in quotes and ("") etc to no avail. I get "undefined" if it works – Dfoles Feb 19 '16 at 20:24
  • @Dfoles : You tried using it as : name=' + [01]home_room_teacher + ' – stark Feb 19 '16 at 20:25
  • I'll have to try that when I get back to a computer. – Dfoles Feb 19 '16 at 20:28
  • So would I be able to add that to the .after (.after.html())? – Dfoles Feb 19 '16 at 20:37
  • @Dfoles : Yes you'd be able to add it to your code, basically instead of `$j("tr:contains('Home Room')").after('Home Room Teacher');`, just write : `$j("tr:contains('Home Room')").after('Home Room Teacher'); ` , and the code should work fine then. – stark Feb 19 '16 at 20:38
  • Still no go :( I even tried doing name="'+[01]home_room_teacher+'" – Dfoles Feb 22 '16 at 17:54