1

I kind of call a variable inside of a .html(); but it does not function properly.

var cap2 = "Here is a muddy truck.";
id = $(this).attr("id");
$("#caption").html("cap"+id); // this literally outputs "cap2" into my caption div

Obviously this is not the complete code but I think its enough for someone knowledgable to point me in the right direction.

I should also note when the last line looks like this

$("#caption").html(cap2); // this correctly outputs "Here is a muddy truck"

all is well but I need the numeric slot to be dynamic. Any thoughts?

4
  • Post more of the code; both HTML and JavaScript. So that we can help troubleshoot. Commented Oct 29, 2012 at 18:45
  • I would like to know what $(this) is referring to? Commented Oct 29, 2012 at 18:47
  • 2
    There are better ways to do this, like not using just a number for an ID, and if you have multiple strings, putting them in an array and referencing an array value instead of trying to eval a bunch of different variablenames etc. Commented Oct 29, 2012 at 18:49
  • More code probably isn't needed. $(this) is likely referring to an element accessed through jQuery and isn't relevant other than that he is getting its ID attribute. Commented Oct 29, 2012 at 18:50

7 Answers 7

4

If your cap2 variable is declared globally, you can use:

$("#caption").html(window["cap"+id]);
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using this solution. This solution only worked after declaring my capX variables outside of my $(document).ready function. Thank you works as I needed.
0

You have to use the 'evil' eval function:

$("#caption").html(eval("cap"+id)); 

Evil because it can do all sort of dangerous things!

1 Comment

If the OP has complete control over the value of id then this should be safe enough. But if there is any possibility of user entered data coming in the id part, avoid eval
0

In javascript '+' is used to concatenate strings, and since the function receives on string parameter, you are sending it "cap2" as a string, and not the variable.
The eval() method will take the string you give it and run it as if it was code, hence, sending the 'cap2' variable to the .html() method.

$("#caption").html(eval("cap"+id));

Comments

0

The javascript function eval takes a string a evaluates it as a javascript variable, function, etc. Use .html(eval("cap"+id));

Comments

0

Just an example of how stuff like this would normally be handled:

<a href="#" id="a0"></a>
<a href="#" id="a1"></a>
<a href="#" id="a2"></a>
<a href="#" id="a3"></a>

$('a').on('click', function(e) {
    e.preventDefault();
    var caps = [
                "Here is a muddy truck.",
                "Here is a strange duck",
                "Here is the proper code",
                "Here's Johnny"
                ],
         id = this.id.replace('a','');

    $("#caption").html(caps[id]);
});

Preferrably declaring the array outside the click function, but it's just an example!

Comments

0

Adeneo's comment is probably the most helpful until now. If you have to reference variables with different numbers, make an array in the right order and call the index:

var caption = $('#caption'), // no use writing a variable on each action
    caps = [
        "Here is a muddy truck.",
        "another caption"
    ]; // fill with your captions

$(document).on('click', 'a', function() {
    caption.html(caps[this.id]);
});

Comments

0

Why not just stick your strings into a hash? That way you don't have to do any arithmetic on the id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.