2

I am trying to retrieve the first two characters of a link's title attribute, store it into a variable, and then call that variable as a function. In the code, where it says "#" + s, is where I want that value to be placed. Any ideas?

$(".flow").click(function() {
    if (labelstatus = 1) {
        $('.rmflow a,.weflow a,.scflow a,.coflow a,.wcflow a').css({
            color: "#b7b7b7"
        });

        $("." + this.title + " a").css({
            color: "#3e3e3e"
        });

        $("#" + this.title).parent().animate({
            opacity: '1'
        });

        $('.initiate').animate({
            opacity: '.4'
        }, 100);

        // variable to be put in the below selector
        $("#" + s).parent().animate({
            opacity: '1'
        });

        $('.info').fadeOut(200);
        $("#" + this.title).fadeIn(1000);
        return false;
    }
    else {
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
dylfreak6494
  • 109
  • 3
  • 3
  • 8

3 Answers3

20

get the first 2 characters in the title: var s = $(this).attr("title").substr(0, 2);

Marius Ilie
  • 3,283
  • 2
  • 25
  • 27
  • THanks! works perfectly. Does the substr function just call the string of values, and then 0,2 is the range of values to select? – dylfreak6494 Jan 25 '12 at 16:21
3

Get first two of attribute and find target based on it.

$('.flow').click(function() {
    var first2 = $(this).attr('customatt').substr(0, 2);
    $('#' + first2).doSomething();
    return false;
});
Brent Anderson
  • 966
  • 4
  • 6
-1

To extract characters from the end of the string or a start position. Substr is used. Substr is a method that extracts parts of a string from a beginning or ending position, we used this substr() method to extracts specific number of characters.

  • To Extract the First few characters we use substr method with two parameters, the second parameter will be used to return the specific character we want. for example
let String = "ABCDEF"

String.substr(0,3); 
// get only first Character 
// output => ABC
  • To Extract the First few characters we use substr method with one parameters, that the parameter will be used to return the specific character we want. for example
let String = "ABCDEF"

String.substr(3); 
// get last 3 Character 
// output => DEF
Mohamed Raza
  • 818
  • 7
  • 24
  • Using .substr like this has already been mentioned in the other answers. *When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.* – Eric Aya Nov 09 '21 at 16:44
  • this one is the fundamental concept of fetching or removing characters from a string. google indexing this question for a long time for substring query, whenever someone looking to solve this issue. this answer might help them. – Mohamed Raza Nov 09 '21 at 17:11
  • You must explain your answer ! – c-sharp-and-swiftui-devni Nov 10 '21 at 02:58