Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery select first letter?

I am simply attempting to get jquery to identify the first letter of a paragraph. How would I do this?

For example, I have a page with a number of paragrahs on a page. I would like only the paragraphs starting with a specified letter to be given the class "current" and all others to be hidden. I pretty much know how to add the class and hide the others but, I cant get jquery to recognize the first letter.

Secondly, is it possible to pull this 'first letter' variable from the url string?

For example, Page 1 - There is a list of letters. The user clicks 'B' and the url is

http://domain.com/page2.html?letter=b

And page2 picks up that variable (b) and applies it to the Jquery, showing only those paragraphs

like image 916
Batfan Avatar asked Jun 14 '10 17:06

Batfan


People also ask

How do I get the first letter of a word in jQuery?

You should use the charAt() method at index 0 for selecting the first character of the string. The substring() Method. You can also use the substring() method to get the first character: The slice() Method.

How do I get the first letter of a string?

Get the First Letter of the String You should use the charAt() method, at index 0, to select the first character of the string. NOTE: charAt is preferable than using [ ] (bracket notation) as str.

How do I get the first character of a textbox in jQuery?

You can use charAt to get the first letter. var x = 'some text'; alert(x. charAt(0)); If you're using jQuery and have a textbox with id="firstName" you can access the first letter in the following way.

How to check the first character of a string in js?

To get the first character of a string, we can call charAt() on the string, passing 0 as an argument. For example, str. charAt(0) returns the first character of str . The String charAt() returns the character of a string at the specified index.


2 Answers

Try:

jQuery('p').each(function(){     
    if(jQuery(this).text().substr(0,1).toUpperCase() == 'B'){
         jQuery(this).addClass('someclass')
    }
   })

You can use PHP to clean the variable and the print it in JS:

<script type="text/javascript">
var letter = '<?php  echo (strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>'
</script>

Or just grab it with document.location and extract it.

like image 87
Aaron Harun Avatar answered Sep 22 '22 21:09

Aaron Harun


If you'd like to use JavaScript to grab the letter from the URL query string, run a regular expression on window.location.search:

var letterParam = window.location.search.match(/letter=([a-z])/i), letter;

if (letterParam)
{
    letter = letterParam[1];
}

To match paragraphs starting with that letter, use the charAt() method in JavaScript strings:

if (letter)
{
    $('p').each(function()
    {
        if ($(this).text().charAt(0).toUpperCase() == 'B')
        {
            // Apply the CSS class or change the style...
        }
    });
}
like image 37
BoltClock Avatar answered Sep 19 '22 21:09

BoltClock