Skip to content Skip to sidebar Skip to footer

JQuery Html() And &

I'm doing a search within list of people. And I want it to show results on the fly and so it does. But there is one link that I need and it should look like this: chatid=18&use

Solution 1:

You said you are trying to set the link path correct?
If so try this

$(".rBoxContentStaff").attr("href", data);

the .html() is changing the & to an html & (&) but using .attr("href") sets the link path and it works with &s as well


Solution 2:

Instead of doing this:

$(".rBoxContentStaff").html(data); });

Try this:

$(".rBoxContentStaff").append(data); });

text() escapes html characters ... I can't find anything about html() escaping characters (and indeed, its documentation seems to indicate otherwise.
However, after testing with alert at jmein's suggestion, it does encode special characters. Append() does not do so.


Solution 3:

html special chars when requested by ajax, don't get interpreted by the browser .. that's why the links came out that way. One thing you can do is a sstring replace in javascript:

data = str.replace(/&/, "&");

Solution 4:

You should try using the $.ajax() function instead of $.get().

With $.ajax() you can specify your datatype yourself (and use other options).

Try something like this:

$.ajax({
type: "GET",
url: "/ajax.php",
data: "sec=search_users&ajax=1&search_for="+$(this).val(),
cache: false,
dataType: "html",
success: function(html){
    $(".rBoxContentStaff").html(html);
} });

Solution 5:

And there's also the way to convert getting just the text (the <a /> is just a helper):

$('<a /'>).html(data).text()

This will move &amp; to &, and also all other special characters like &imp; etc...


Post a Comment for "JQuery Html() And &"