1

I need to pass to javascript function some data that has special characters like

#228;

A full string will look like this

Bundesland Kärnten

My javascript function looks like this

function my_fumc(arg){
    console.log(arg);
} 

If i call this function in the following way

my_fumc('Bundesland Kärnten')

I get in console

Bundesland Kärnten

Instead of

Bundesland Kärnten

So, the

Kä

being replaced.

I tried different things to solve this problem. 1.Passing a variable as parameter

my_var = 'Bundesland Kärnten';
my_fumc(my_var)

It works but i don't think it's a good solution.2. I also tried using encodeURI() function like

my_fumc(encodeURI('Bundesland Kärnten'))

As a result i get:

Bundesland%20K%C3%A4rnten

I prepared a very simple jsfiddle to show how javascript behaves when passing special characters to function.

Please, help me find a solution.

user2265529
  • 479
  • 1
  • 8
  • 18
  • is it possible for you to set this string value ("Bundesland Kärnten") to some data- attribute of button? – Akshay Chawla May 01 '17 at 08:48
  • Possible duplicate of [Encode html entities in javascript](http://stackoverflow.com/questions/18749591/encode-html-entities-in-javascript) – Kaiido May 01 '17 at 08:54
  • This might be browser-specific...it's interesting that it treats the string literal differently if it's put in a variable first, because on Chromium for Linux they both display the same result (with the ä). – Kev May 01 '17 at 08:57
  • 1
    This is not how js behaves, but how html does. Rule of thumb : don't use html `onEvent` attributes, for reasons like this. If you simply did `button.addEventListener("click", function(){myfunc('Bundesland Kärnten')});` you wouldn't have this problem. – Kaiido May 01 '17 at 08:58
  • @Akshay Chawla as you advised i tried data- attribute but i still doesn't work as it should. See jsfiddle https://jsfiddle.net/aucrw5hx/ – user2265529 May 01 '17 at 09:03
  • @user2265529 are you try with `decodeURI()` for decode your string – prasanth May 01 '17 at 09:05
  • is there any restriction to use jquery ? – Akshay Chawla May 01 '17 at 09:22
  • @Akshay Chawla jquery can be used – user2265529 May 01 '17 at 09:24
  • when you run your page, just look at that specific button element using 'Inspect element'. Issue is with HTML only, that specific special characters get converted into alphabets when HTML has been rendered, so that's why JavaScript is printing alphabets. – Akshay Chawla May 01 '17 at 09:39
  • is it possible for you to assign this string value directly into an JavaScript variable ? – Akshay Chawla May 01 '17 at 09:40
  • @Akshay Chawla ,It is one of the methods that i may use if no simpler solutions will be found. – user2265529 May 01 '17 at 09:51

1 Answers1

0

As pointed out by Kaiido, the problem is more to do with how HTML works.

An option might be something more like:

var txt = ["Bundesland Kärnten 01", "Bundesland Kärnten 02"];

function my_func(e){
 var idx = (this.id == "t1") ? 0 : 1;
 console.log(txt[idx]);
}

window.onload = function() {
 document.getElementById("t1").addEventListener("click",my_func,true);
 document.getElementById("t2").addEventListener("click",my_func,true);
}
<button id="t1">button 1</button>
<button id="t2">button 2</button>
Tigger
  • 8,980
  • 5
  • 36
  • 40