-1

My html attribute is like below:

<span>
  <input 
    class="editWidgetName" 
    type="text" 
    maxlength="100" 
    value="Untitled "NAME" 30\10\2017" 
    style="display:none;padding-right:1px;"
    />
</span>

I can't fetch the value of the above 'editWidgetName' class. It's always fetching 'Untitled ' only. The string after '"' portion is getting omitted. I tried javascript to escape the '"' like: value.replace(/\"/g, '\\"') , but the result is same.

Again, I tried with encoding the value attribute using js function like:

function encodeHTML(s) {
    return s.split('&').join('&amp;')
            .split('<').join('&lt;')
            .split('"').join('&quot;')
            .split("'").join('&#39;');
}

but result is fetching as 'Undefined'.

Please suggest another way to resolve this. Thanks.

Prosen
  • 72
  • 1
  • 11
  • 1
    can you create a snippet using `<>` showing how you are using this method `encodeHTML`? – gurvinder372 Oct 30 '17 at 06:26
  • How are you setting `s` (in your function `encodeHTML()`) ? – Calaris Oct 30 '17 at 06:28
  • 1
    `value="Untitled "NAME" 30\10\2017"` is maybe the problem. You have to escape the inner double quotes. –  Oct 30 '17 at 06:29
  • I used like: *var name = 'Untitled "NAME" 30\10\2017';* ** Yes, I also tried escaping the inner double quote like: *name.replace(/\"/g, '\\"')*, but result is same. – Prosen Oct 30 '17 at 06:33
  • @Quentin , this question is different than the marked question one, because as I already mentioned in my question that I've used those solution but that didn't work in my scenario. Any more suggestions? – Prosen Oct 30 '17 at 09:00
  • @ProsenjitDutta — They should work. It sounds like your attempt to implement them was wrong. Try providing a complete [mcve]. – Quentin Oct 30 '17 at 09:24

1 Answers1

0

Solve a problem means nothing. Take good habits is the solution.

@ProsenjitDutta, make your best to separate the logic, the style and the container. As a start, you could:

  • Never use the style attribute. Use CSS instead, with CLASSes and IDs attributes.
  • Never write the value in the HTML code. Use Javascript instead (or templates).
  • Take a time to find the best method (SO is your friend). To repeat split/join is a bad practice (slow and ugly), use Regexp instead.

// Maybe typo here (fixed)----v   (backslash)

let name = 'Untitled "NAME" 30/10/2017'; 

// With jQuery:

$('#editWidget').val(name) ;

// Without jQuery

document.querySelector('#editWidget').value = name ;
/**
  * Don't hard code the style in the tag
  * Use CSS instead (as below)
  *
  */
input.editWidgetName {
  width: 200px;
  padding-right: 1px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>
<!--          v---------- an ID here                 -->
  <input id="editWidget" value="" class="editWidgetName" type="text" maxlength="100" />
<!--    empty value --------^                        -->

</span>

function encodeHTML(s) {
  return s.replace(/([&<"'])/g, function(match, char){
  switch(char){
    case '&': return '&amp;';
    case '<': return '&lt;';
    case '"': return '&quot;';
    case "'": return '&#39;';
  }
})}

let s = "var=val<pour\"voir'ca'" ;

console.log('s = ', encodeHTML(s));
.as-console-wrapper{max-height:100%!important;top:0;}
  • Thanks a lot for your valuable suggestion. I will definitely follow as you have suggested. Thanks once again. – Prosen Oct 30 '17 at 07:09
  • Yes @Philicare I already tried to upvote as good answer, but as I've newly created this account, my current reputation is below 15. Hence my vote is not showing publicly. Thanks a lot. – Prosen Oct 30 '17 at 08:56
  • You're right! Thanks!;-) Don't forget to remove the unecessary comments under the answers/question (SO don't like the "thanks" ;-)) –  Oct 30 '17 at 09:00
  • @Philicare, can you please upvote my question so that I can avoid blocking my account? Thanks a lot in advance. – Prosen Nov 08 '17 at 06:19