8

I have to create:

  • 1 <input type="text">
  • 1 <textarea>
  • 1 <div>
  • 1 <button>

I have to fill the div with the textarea's content but if the content contains the input's string, I have to color it with <span>.

For example:

If the input contains "is" and the textarea contains "this is a beautiful day", I should put the following text in the div "this is a beautiful day" and color every time that the "is" string appears.

I should use indexOf() and a loop.

I have this:

var a = $("#inp1").val();
var b = $("#txt").val();

var x = b.indexOf(a);
    if (x > -1)
7
  • where is the div, the button, the textarea and the input? Commented Apr 22, 2015 at 12:57
  • You now know the position of b in a. You can split your string and recreate it, your b wrapped in a styled span. w3schools.com/jsref/jsref_substring.asp Commented Apr 22, 2015 at 12:58
  • 1
    color what? The matched word or all the text. Commented Apr 22, 2015 at 12:58
  • @rikpg For what do you need it? Commented Apr 22, 2015 at 12:58
  • 1
    homework can be tough, eh? :/ Commented Apr 22, 2015 at 13:16

7 Answers 7

3

If you absolutely have to use indexOf

while(b.indexOf(a) != -1) {
   b = b.replace(a, '[X]');
}
while(b.indexOf('[X]') != -1) {
   b = b.replace('[X]', '<span style="color:yellow;">' + a + '</span>');
}
$("#targetDiv").html(b);

You can also do this with RegExp

var a = $("#inp1").val();
var b = $("#txt").val();
var re = new RegExp(a, 'g');
var divContent = b.replace(re, '<span style="color:yellow;">' + a + '</span>');
$("#targetDiv").html(divContent);

Here is a fiddle with the indexOf

http://jsfiddle.net/eva3ttuL/1/

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain me what is the ['x'] refers to?
It's just a placeholder, since you wanted to use indexOf. Since I'm replacing a string with the same string in a span element; it would be an infinite loop :)
if i replace the string with the same string in a span element ( .replace(a, '<span style="color:yellow;">' + a + '</span>'). why would it be an infinite loop?
Because indexOf will find the a string again, since you put it in there again :)
2

Here is the code to find and change color of all searched word

$(document).ready(function () {
    $("#here").on("keydown keyup", function () {
        var mytext = $(this).val();
        var find = $("#find").val();
        mytext=mytext.replace(new RegExp(find,"g"), "<span class='me'>"+find+"</span>");
        $("#output").html(mytext);

    });
})
  .me{color: #00f;
  background-color: #EFFF00;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="find" type="text" /><br/>
<textarea id="here"></textarea><br/>
<div id="output"></div>

Comments

0

Use JavaScript substring to split the string inside textarea and form a paragraph with the highlighted text.

HTML code:

<input id="inp1" type="text"/><br>
<textarea id="txt" col="10" row="5"></textarea><br>
<div id="fillarea">
    click on check
</div>
<button id="check">check</button>

CSS for highlighting:

.highlight{
    background-color:yellow;
}

jQuery code:

$('#check').on('click',function(){
    var a = $("#inp1").val();
    var b = $("#txt").val();
    var x = b.indexOf(a);
    var first = b.substring(0,x);
    var middle = b.substring(x,x+a.length);
    var last = b.substring(x+a.length,b.length);
    var to_print = '<p>' + first + '<span class="highlight">' + middle + '</span> ' + last + '</p>';
    $('#fillarea').empty();
    $('#fillarea').append(to_print);
});

Demo fiddle here

2 Comments

that could be good but notice that if i write in the input: "ab" and in the the textarea: "deabchab", only the first "ab" is highlight. I have to highlight everytime that "ab" appears
@moral17592 I didn't notice that. Thanks :)
0

It helps you jsfiddle

$('.submit').click(function(){
    var content = $('.textarea').val();
    var ans = content.replace($('.input').val(), "<span style='color:red;'>"+$('.input').val()+"</span>");
    $('.text').html(ans);
});

1 Comment

Apparently, the poster is supposed to use indexOf(). Also, this only highlights the first instance of $('.input').val().
0
var input = $("#inp1").val();
var text = $("#txt").val();
var div = $("#div");

div.val(text.replace(new RegExp(input,'g'), makeSpan(input)));

function makeSpan(str) {
  return '<span style="background-color:red">' + str + '</span>';
}

Comments

0

Just try this

<html>
  <title></title>
  <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
  $(document).ready(function() {
   var a = "is";
   var b = "this is beautiful";
    
    var x = b.indexOf(a);
    
     if (x > -1) {
      var re = new RegExp(a, 'g');
      res = b.replace(re, "<span style='color:red'>"+a+"</span>" );
      $("#result").html(res);
     }
  })
 
 </script>
    </head>
<body>
<div id="result"></div>
</body>

Comments

-1

You can use IndexOf Javascript function

var str1 = "ABCDEFGHIJK";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    alert(str2 + " found");
}

1 Comment

have dare to face then comment here.....without giving Explanation You have no right to do this.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.