13

In Ruby you can use string interpolation like so:

text = "This is visit number #{numVisits} to this website"

This bypasses the need for explicit concatenation.

I'm working with jQuery and have a bit like this:

$(document).ready(function(){
    $("a.ajax").click(function(event){
        $("#content").load("data.html this.getClass");
    });
});

The behavior I want is "click on <a class="ajax" id="schedule"></a> and the content div on the current page is replaced by the schedule div from data.html. If I manually write in

load("data.html #test"); 

that works, but I want the script to load the DIV with the ID value of the anchor clicked. Any help would be swell!

Example Page: http://www.mariahandalex.info/stack/

3
  • What value do you want to pass the load method? Commented May 10, 2009 at 19:54
  • The phrase you are looking for is variable interpolation, I'm afraid Javascript doesn't have such a feature. en.wikipedia.org/wiki/Variable_interpolation#Interpolation Commented May 10, 2009 at 19:56
  • To clarify, I have 6 links, each with a unique ID tag. When I click on a given link, I want that link ID to get passed to the load function: load("data.html #(clickedlinkID); Commented May 10, 2009 at 20:05

5 Answers 5

17

This has changed.

As of 2015 there is now a better way: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

Bringing to JS what many languages like Ruby and PHP have enjoyed already.

For example, in my jQuery driven page I use:

   $('#myModalLabel').html(`Show GRAPH of : ${fundCode}-${funcCode}`); 

Which safely renders in my updated Firefox and Chrome as:

Show GRAPH of : AIP001-_sma

Note the use of backticks surrounding the string param in .html(....)

Sign up to request clarification or add additional context in comments.

1 Comment

Oh neat, I haven't seen that yet. Huge red flag on the browser reqs, but nice to know about! Thanks, 6 years later... :-)
6

You cannot embed javascript code inside a string, but you can concatenate a string to an object using '+' like so

$('#content').load('data.html #' + $(this).getClass());

About embedding code in a string, I don't think it has a name. Groovy calls those strings "Gstring"s, and ruby calls those 'Expression substitution in strings'. I'm not sure it has a standard name.

1 Comment

I think you mean $(this).getClass() or maybe this.className.
5

Javascript doesn't parse string literals looking for replacement markers like Ruby or PHP does. You will have to use concatenation.

Comments

5

in es6 this is possible

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

`string text ${expression} string text`

Comments

2

Try this:

$(document).ready(function(){
    $("a.ajax").each(function(){
        var obj = $(this);
        obj.click(function(event){
           alert(obj.attr('id'));
        });
    });
});

5 Comments

@Alex Mcp: Take a look at it now.
'#' + this.getClass()? you probably meant '.'
But that will append the Class of the "#content" div. That is where the data is getting shuttled in and out of. I need to find a way to tack on the "ID" attribute of the link that was clicked, because that will identify what gets pulled out of the external page.
I forgot that it’s an jQuery object that gets returned. It works now.
I put an example page up. This is so strange, it's only returning the id value for the first in the list of links. Maybe the list is somehow the object, and it thinks it only has one ID? I'm a bit beyond my knowledge here... Thanks so much.

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.