views:

45

answers:

4

I have attribute value as:

<div id = "2fComponents-2fPromotion-2f" class = "promotion">

Now I want to get only portion of it, say Promotion and its value 2f, how can I get this using jquery ? Do we have built in function for it ?

+3  A: 

You can use a regular expression here:

var attId = $(".promotion").attr("id");

// Perform a match on "Promotion-" followed by 2 characters in the range [0-9a-f]
var match = attId.match(/Promotion-([0-9a-f]{2})/);

alert(match[1]); // match[0] contains "Promotion-2f", match[1] contains "2f"

This assumes that the "value" of Promotion is a hexadecimal value and the characters [a-f] will always be lower case. It's also easily adjusted to match other values, for instance, if I change the regex to /component-([0-9a-f]{2})/, the match array would be ["component-3a", "3a"].

The match method takes a regular expression as its input and searches the string for the results. The result is returned as an array of matches, with the first index being the complete match (equivalent regex for this only would be /Promotion-[0-9a-f]{2}/). Any sub-expression (expressions enclosed in parenthesis) matches are added to the array in the order they appear in the expression, so the (Promotion) part of the expression is added to the array at index 1 and ([0-9a-f]{2}) is added at index 2.

match method on MSDN

Andy E
Can I get value as Promotion and 2f separately ?
Rachel
@Rachel: of course, all you have to do is put braces around Promotion in the regular expression: `/(Promotion)-([0-9a-f]{2})/`. This will change the array format slightly, `match[1]` would be `"Promotion"` and `match[2]` would be `"2f"`.
JJ
@Rachel: yes, just put parenthesis around whichever value you want to match in the regular expression and it will be included in the `match` array.
Andy E
Currently I am getting match as array of `Promotion-2f,Promotion,2f` can you explain how is this regular expression working and also if I want to get Promotion only than what would be the regex for it...am bit weak in regex and so kindly advise
Rachel
@Rachel: I've added an explanation, as best as I can phrase one right now, to my answer. Are you wanting to match `Promotion` without actually specifying the word `Promotion` in the regular expression?
Andy E
@Andy E's head: Thank you for the explanation and yes actually I was looking for the Promotion without actually specifying the work Promotion in the regular expression
Rachel
@Andy E's head: How can I get the value of Promotion i.e. `2f` using regular expression ?
Rachel
+1  A: 
var id = $("div.promotion").attr("id");
var index = id.indexOf("Promotion");
var promotion = '';

// if the word 'Promotion' is present
if(index !== -1) {

    // extract it up to the end of the string
    promotion = id.substring(index);

    // split it at the hyphen '-', the second offset is the promotion code
    alert(promotion.split('-')[1]);
} else {
    alert("promotion code not found");
}
karim79
Can you explain working of this as it is not clear right now.
Rachel
@Rachel - I have added in comments
karim79
Instead of having the extra check for "Promotion", I would just include it as part of the selector: `$('div.promotion[id*=Promotion]').attr('id');`
fudgey
@fudgey - good point.
karim79
A: 

you can get the id attribute like this:

var id= $('div.promotion').attr('id');

But then I think you would have to use regular expressions to parse data from the string, the format doesn't appear to be straight forward.

If you are storing lots of info in the id could you consider using multiple attributes like:

<div class="promotion" zone="3a-2f-2f" home="2f"></div>

Then you could get the data like this:

var zone= $('div.promotion').attr('zone');
var home= $('div.promotion').attr('home');

Or you could use jQuery.data()

HTH

DannyLane
I cannot create new attributes, can you explain how can I get values out from my current attribute value using jQuery.data() ?
Rachel
jQuery.dat() won't help with the id you are using, its an alternative to adding attributes in the the markup, you can let jquery store the data on the element. I think regex is the only option for your current ID format.
DannyLane
A: 
 $(function () {

        var promotion = $('.promotion').attr('id').match(/Promotion-([0-9a-f]{2})/);

        if (promotion.length > 0) {

            alert(promotion[1]);
        }

        else {

            return false;
        }

    });
XGreen