tags:

views:

45

answers:

3

So I have captured the ID of an image in a jquery variable like so:

$("#tag img").click(function(e) {       
    var element = $(this).attr("id");

How do I then parse the var element?

I would like to put it in an if statement like this:

if ( $(element *= 'square' ) {
    $('#tag #view_name').text("Tag with a Square");
}
elseif ($(element *= 'circle' ) {
    $('#tag #view_name').text("Tag with a Circle");
} 

How do I do that, with the least amount of code possible?

Thanks.

Edit: Oh, and the text I am searching for are the two strings in the single quotes ('square' and 'circle').

Edit: All of the answers below look like they should be working...but they aren't. Here is the other code, so you guys can see if I am missing something:

<div id="tag">
        <a href=""><img src="images/square.png" id="square-tag"></a> | 
        <a href=""><img src="images/circle.png" id="circle-tag"></a> |      
        <span id="view_name">Tag with Square</span>
    </div>  

Edit 3: Ok...it seems that some other jQuery was hijacking that div. All the answers are right. I wish I could mark all of them correct :)

+2  A: 

This is similar to $("input[name*='man']") selector (example from your comment)

var name = $(this).attr("name");
if (name.indexOf('square') >= 0) {
    $('#tag #view_name').text("Tag with a Square");
}
Nikita Rybak
Cool! Do you have a URL that explains this more?
marcamillion
This doesn't work.
marcamillion
@marcamillion Example here: http://jsfiddle.net/DGY5x/
Nikita Rybak
@marc - There's no reason for 2 IDs in a selector, it's very inefficient :)
Nick Craver
@Nick you are right. I will have to re-arrange a few stuff to fix it. Had the issue of multiple elements (i.e. the exact same span on another page) with the same ID.
marcamillion
+1  A: 

Try this:

if (element.indexOf("square") != -1)
    $('#tag #view_name').text("Tag with a Square");
else if (element.indexOf("circle") != -1)
    $('#tag #view_name').text("Tag with a Circle");
SimpleCoder
This doesn't work either.
marcamillion
You must be doing something wrong, because these methods we have all posted shouldn't fail for any reason.
SimpleCoder
It works now...my bad. There was another piece of JS that was hijacking that ID.
marcamillion
+3  A: 

From a minimal code standpoint, this would work:

$("#tag img").click(function() {  
  if (this.id.indexOf('square')>-1) {
    $('#view_name').text("Tag with a Square");
  } else if (this.id.indexOf('circle')>-1) {
    $('#view_name').text("Tag with a Circle");
  }
});

When selecting by ID use only the ID, it's a faster selector as well. I'm doing it this way because of how your question's phrased, for this specific example, it's a bit cleaner to do:

$("#tag img[id*='square']").click(function() { 
  $('#view_name').text("Tag with a Square");
});
$("#tag img[id*='circle']").click(function() { 
  $('#view_name').text("Tag with a Circle");
});
Nick Craver
Looks like it should work....but it's not agreeing with my code - for some reason. I have updated my original question with more HTML for troubleshooting purposes.
marcamillion
@marcamillion - Here's a demo with the code, note the added `href` so the page doesn't redirect: http://jsfiddle.net/nick_craver/efEBd/
Nick Craver
Thanks Nick. Got it. Silly me...something else was hijacking that div's click(function()) :|
marcamillion