• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript indexOf – Get the position of a value within a string

JavaScript indexOf – Get the position of a value within a string

December 27, 2021 Leave a Comment

We can use the JavaScript indexOf method to get the starting position of a value (String or character) within a string. If the value is not found within the string, -1 is returned.

"Text".indexOf('x');

This would result in the following output:

2

Some other examples of the indexOf() method are below:

var text = "Example text";
var str1 = text.indexOf('e');
var str2 = text.indexOf('E');
var str3 = text.indexOf('text');
var str4 = text.indexOf('q');
var str5 = text.indexOf('e',2);
var str6 = text.indexOf('Example');
var str7 = text.indexOf('Examples');

Which would result in the following:

6
0
8
-1
6
0
-1

The main things to remember with the JavaScript indexOf method are that the first character starts at index 0, not 1 and that the characters are case-sensitive.

An example using the JavaScript indexOf Method

Below is an example of replacing an image using JavaScript indexOf() and other JavaScript methods.

<img id="img1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
<div id="click-me">Change image</div>

Let’s say we have another image called “example-img2” which is in the same location as the image above. When we click the button, we want to swap out that image. We will use the JavaScript indexOf method to get the location of “example-img1” and use the substring() method to get the first part of the image location. Then we will add the new image file name and replace the old image. Here is the JavaScript code below:

$("#click-me").click(function(){
  var oldImgAddress = $('#img1').attr('src');
  var indexOfImage = oldImgAddress.indexOf('example-img1.png');
  var firstPartOfString = oldImgAddress.substring(0,indexOfImage);
  var newImgAddress = firstPartOfString + "example-img2.png"; // we add the new file name to the end of the string
  //This new string should now be "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png"
  //We can now use jQuery to replace the old image  
  $('#img1').attr('src',newImgAddress);
});

The final code and output for this example is below:

Code Output:

Change image

Full Code:

<img id="img1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
<div id="click-me">Change image</div>

<script>

$("#click-me").click(function(){
  var oldImgAddress = $('#img1').attr('src');
  var indexOfImage = oldImgAddress.indexOf('example-img1.png');
  var firstPartOfString = oldImgAddress.substring(0,indexOfImage);
  var newImgAddress = firstPartOfString + "example-img2.png";
  $('#img1').attr('src',newImgAddress);
});

</script>

Hopefully this article has been useful in helping you understand the JavaScript indexOf() method.

Other Articles You'll Also Like:

  • 1.  Check if a String Contains Uppercase Letters in JavaScript
  • 2.  Using Javascript to Remove Class from Element
  • 3.  How to Create a JavaScript Count Up Timer
  • 4.  Using JavaScript to Check if Variable is an Object
  • 5.  Using JavaScript to Get the Base URL
  • 6.  How to Clear an Input Field in JavaScript
  • 7.  How to Swap Values in an Array in JavaScript
  • 8.  Using JavaScript to Remove All Non-Alphanumeric Characters From a String
  • 9.  How to Change the Id of an Element in JavaScript
  • 10.  JavaScript String endsWith Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x