Erty Seidohl
Erty Seidohl

Reputation: 4559

Jquery string filtering

I'm doing a little fancy snippet of js to automatically copy the value of a textbox into a later span.

<input name="data[ask_admin_name]" onkeyup=
    "$("#admin_name").html($(this).val())"
type="text" id="ask_admin_name">

and then, later,

<span id="admin_name"></span>

What is the fastest, easiest, most jquery-est way to strip html-related tags out of this, like < and >?

I could do it using some more lines of js, but I'm wondering if there's a specific function for this task, for example, $(this).val().strip('<>');?

Upvotes: 1

Views: 72

Answers (2)

VisioN
VisioN

Reputation: 145398

Use text() method instead:

<input onkeyup="$('#admin_name').text(this.value)">

DEMO: http://jsfiddle.net/9Wu4N/

Upvotes: 6

Grzegorz Kaczan
Grzegorz Kaczan

Reputation: 21676

Just create an element from the html you want to strip and then run jQuery .text() method on that element.

$("<p>bla</p>").text() // bla

Upvotes: 1

Related Questions