Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

Monday, March 11, 2024

Convert String to Title case using Javascript

Here is the function to convert string to title case, which can handle spaces and underscores. Below function will remove underscores from the string.

// Import the function
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

You can call the convertToTitleCase function in HTML by including a script tag with the function definition, and then using JavaScript to call the function and display the result.

Here's an example of how you can call the convertToTitleCase function in HTML:

<!DOCTYPE html>
<html>
<head>
  <title>Convert to Title Case</title>
</head>
<body>

<p id="output"></p>

<script>
// Function definition
function convertToTitleCase(input) {
  return input.toLowerCase().replace(/_/g, ' ').replace(/\b\w/g, function(match) {
    return match.toUpperCase();
  });
}

  // Call the function and display the result
  let input = "Nagasai_Srinivas_Mudara";
  let convertedString = convertToTitleCase(input);
  document.getElementById("output").innerHTML = convertedString;
</script>

</body>
</html>

In this JavaScript function, the replace method is used with a regular expression to match the underscores and lowercase letters and convert the lowercase letters to uppercase when preceded by an underscore or at the beginning of the string.

You can use the convertToTitleCase function to convert any input string to title case in a generic and reusable way.

Tuesday, March 05, 2024

How to check if string exists in JQuery

In jQuery, you can use the indexOf method to check if a string contains another string. Here's an example:

var mainString = "Hello, world";
var subString = "world";

if (mainString.indexOf(subString) !== -1) {
    // subString is found in mainString
    console.log("Substring found");
} else {
    // subString is not found in mainString
    console.log("Substring not found");
}
  

In this example, the indexOf method returns the index of the first occurrence of the subString within the mainString. If the subString is not found, indexOf returns -1. You can use this to check if a string contains another string in jQuery.

Sunday, June 05, 2016

Browser support and comparisons

Recently found a amazing site www.caiuse.com which is Useful to web designers/developers.

"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.

Very useful and I recommend this!

Thursday, October 16, 2014

How To Disable Right Click Using jQuery

There are many different ways where we can disable right click on browser but using jQuery its very easy with one line of code.

Add the below code into the head section of your web page.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).bind("contextmenu", function (e) {
e.preventDefault();
});
});
</script>