0

I don't know how to name it, but I'll try to describe.

$(document).on('click','.swaction',function(event){
    var pdfId = $(this).closest('tr').find('td:first').html(); // Example: 44!F=%7214
    var type = $(this).closest('tr').data('type');
    var tableId = $(this).closest('table').attr('id');
    var currentElement = this;
    event.preventDefault();

    if( $(currentElement).data('action') === 'get' )
    {
      window.location.href = "/Printbox.php/ct_form_procesar_escaneos/pdf/get/" + type + "/" + pdfId;
    }

    if( $(currentElement).data('action') === 'rename' )
    {
      $(currentElement).closest('tr').find('td:first').html(
        '<input id="newPdfName-'+pdfId+'" type="text" name="newPdfName" />'+
        '<select id="newPdfType-'+pdfId+'" type="text" name="newPdfType">'+
        '<option selected="selected" disabled="disabled" value="">Tipo de Documento</option>'+
        '<option value="cp">CP</option>'+
        '<option value="tb">TB</option>'+
        '</select>');
      $('#newPdfName-'+pdfId).val(pdfId); // THIS doesn't work. Field is empty.
      $.procesar_escaneos.linksHtml = $(currentElement).parent().html();
      $(currentElement).parent().html('<a class="swaction" data-action="save" href="#">Guardar</a> | '
        + '<a class="swaction" data-action="cancel" href="#">Cancelar</a>'); // This line doesn't seem to execute, the buttons/links are not replaced.
    }

This is working good with numbers and letters, but when pdfId takes a value with (, $, etc the web breaks.

It might seem obviously that there are problems with these characters, but I don't know how to escape them.

Example pdfId = 44!F=%7214

JorgeeFG
  • 5,651
  • 12
  • 59
  • 92

2 Answers2

1

From this answer: HTML-encoding lost when attribute read from input field

function htmlEncode(value){
    //create a in-memory div, set it's inner text(which jQuery automatically encodes)
  //then grab the encoded contents back out.  The div never exists on the page.
  return $('<div/>').text(value).html();
 }

function htmlDecode(value){
  return $('<div/>').html(value).text();
}

Now just wrap your pdfIds with it:

  '<input id="newPdfName-'+ htmlEcode(pdfId)+'" type="text" name="newPdfName" />'

Take note that your attributes will have names other than what you originally anticipated, you will likely need to do a dance with htmlDecode to ensure your script functions.

Community
  • 1
  • 1
Mister Epic
  • 16,295
  • 13
  • 76
  • 147
1

The problem isn't with the id, it's with the jQuery selection, meaning if you'd like to keep the same id without encoding you can use this to change the value:

 $(document.getElementById('newPdfName-' + pdfId)).val(pdfId)

working fiddle here: http://jsfiddle.net/PmVwz/1/

Khodor
  • 996
  • 1
  • 6
  • 13