I’ve got this span:
<span id="inicial"></span>
and this javascript inside a button click:
var pat = /\/\S+\//i;
context = '';
context = $('#cmd').attr('value');
context= context.match(pat);
alert(context); //this gives correctly the string i need
if (context.length){
$('#inicial').text(context); //but it fails to change the text inside the span
}
What could be the problem?
Also i noticed that it affects the whole click function, it just stops working. What could possibly be the cause?
The problem is that
.match()returns an array, not a string. But.text(parm)requiresparmto be a string.So after the
.match(), you should do something like:or use some other methodology to convert at least the first element in the array to a string, if not the full array.
Here’s the reference for
.match(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/matchAnd the ref for
.text(parm): http://api.jquery.com/text/