3

How i can select a character in an HTML element with Javascript or jQuery

For example i want to select the first or the second or the third "]" carchter in <p>

<p> Lorem Ipsum is simply dummy [text] of [the] printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, </p>

And when i write for example console.log(character1) it will give me the first ] in the paragraph that i make

The order of the character is important for me because i want to change the first ']' with something else

Soufiane Douimia
  • 440
  • 3
  • 12
  • Select how, an "A" is an "A" regardless of wether or not it's the first, second of third etc ? – adeneo Aug 09 '15 at 15:13
  • It will find A and print A, is that what you want? – dpanshu Aug 09 '15 at 15:15
  • The order of the character is important for me because i want to change the first 'A' with something else – Soufiane Douimia Aug 09 '15 at 15:16
  • I just guessing here, I think he wants to retrieve all the occurence of a given substring and the relative indexes, if that is the case have a look at this http://stackoverflow.com/questions/3410464/how-to-find-all-occurrences-of-one-string-in-another-in-javascript – oiledCode Aug 09 '15 at 15:22

3 Answers3

2

Try utilizing String.prototype.replace() , increment a counter to select second "A" , any case

var index = null;
var arr = $("p").text().replace(/a/ig, function (m, i) {
    ++index;
    return index === 2 ?  "QQQ" : m
});
$("p").text(arr);

http://jsfiddle.net/srLh93xt/4/

guest271314
  • 1
  • 15
  • 104
  • 177
  • are you conscious that your code is wrong? and you also provided a fiddle :) – oiledCode Aug 09 '15 at 15:27
  • @elio.d _"are you conscious that your code is wrong? and you also provided a fiddle :)"_ Can indicate what portion is "wrong" ? stacksnippets appear not currently functioning ? – guest271314 Aug 09 '15 at 15:34
  • the first solution you provided, does not solve the problem of finding the substring index. match will just return all the matches – oiledCode Aug 09 '15 at 15:49
  • @ guest271314 It works perfectly but when i replace the 'A' with '[' it doesn't work so can you have any solution :) – Soufiane Douimia Aug 09 '15 at 17:24
  • @SoufianeDouimia _"It works perfectly but when i replace the 'A' with '[' it doesn't work so can you have any solution"_ See http://jsfiddle.net/srLh93xt/5/ – guest271314 Aug 10 '15 at 00:25
  • @guest271314 this what i want if you have any solution :) http://jsfiddle.net/srLh93xt/6/ – Soufiane Douimia Aug 10 '15 at 10:47
  • @SoufianeDouimia Try utilizing `RegExp` `/\[/ig` , which escapes opening bracket `[` of special character for character set http://jsfiddle.net/srLh93xt/7/ . _"When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. "_ see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Description , https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Special_characters_meaning_in_regular_expressions – guest271314 Aug 11 '15 at 03:23
0

I understand you need to manipulate a certain element in HTML using javascript

var x = document.getElementsByTagName("p");

this will give you a list of all the nodes that's tagged "p", you can choose yours by specifying its index

y = x[*tag index*]

then you can do:

firstIndex = y.innerHTML.indexOf("A");

or optionally if you want to get the second (or third) index of the letter, you just have to put the start point where the indexof will start looking, so supposedly we want to get the second index of "A" and we already got the first index from the above line of code, we could do:

secondIndex = y.innerHTML.indexOf("A", firstIndex);
smohamed
  • 3,234
  • 4
  • 32
  • 57
0

Well you can do something like this

function getIndicesOf(searchStr, str, caseSensitive) {
    var startIndex = 0, searchStrLen = searchStr.length;
    var index, indices = [];
    if (!caseSensitive) {
        str = str.toLowerCase();
        searchStr = searchStr.toLowerCase();
    }
    while ((index = str.indexOf(searchStr, startIndex)) > -1) {
        indices.push(index);
        startIndex = index + searchStrLen;
    }
    return indices;
}

function replaceCharacter(text ,char, withChar, index) {
    var indices = getIndicesOf(text, char, true);
    return (text.substr(0, indices[index-1]) + 'withChar' + s.substr(indices[index-1]+withChar.length));
}

var text = 'Aekwwewj fkjkwefhwA wkejqwkjeqkwjeA A ewqeqw';
text = replaceCharacter(text, 'A', 'W', 2);

fiddle

oiledCode
  • 8,589
  • 6
  • 43
  • 59