Tim
Tim

Reputation: 7056

javascript indexof within jquery element

I have some code here that looks for every mention of "[code]" and turns it into <code> but I only want to do this within a specific jquery element...

      function formattxt(text){
        if (text == '') return '';

        var start = text.indexOf('[code]');
        var end = text.indexOf('[/code]', start);

...

How would I do that?

I'm looking for something simple like the following:

var start = $("#element").text.indexOf('[code]');
var end =  $("#element").text.indexOf('[/code]', start);

Upvotes: 0

Views: 1919

Answers (2)

steveyang
steveyang

Reputation: 9298

To Enable the replacement, you'd better use replace:

function replaceTag (text) {
  $('#element').text().replace('[' + text + ']', '<' + text + '>')
}

If you want ot use indexOf, use the following as mentioned by @zzzzBov

$('#element').text().indexOf('[code]'); 

Be careful with indexOf, as it is not supported by IE6-8, You need to include thid code to function in all browsers.

Upvotes: -1

zzzzBov
zzzzBov

Reputation: 179046

Read the jQuery api:

$('#element').text().indexOf('[code]');

Upvotes: 5

Related Questions