1

I have a variable being set as the .html(); of a <div />

The variable is the name of another variable. I want it to display the contents of the other variable, however it simply displays the name of the other variable.

How can I force it to show the variable contents rather than just a literal text string?

var clothing = 'dogeshop cryptoshop doge_clothing fyrstikken the_molly_machine peace_and_love moolah_market shibe_swag undieguys urban_graff kravshop got_doge mean_elephant the_dogedoor_store';


setInterval( function() {
    var term = $("input").val();
    $("#results").html(term);
}, 100);

When the user types 'clothing' into $("input"); the contents of the 'clothing' variable should be displayed in the $("#results"); <div />. Instead it just says 'clothing'.

1
  • 1
    I don't think this is the right mechanism. Why not use an associative array instead of a variable? So you'd look up the string 'clothing' (obtained from $("input").val()) and retrieve 'dogeshop cryptoshop...'? Commented Feb 21, 2014 at 21:28

4 Answers 4

1

Another way is to use object properties, like this

var obj = {};
obj.clothing = 'dogeshop cryptoshop doge_clothing fyrstikken the_molly_machine peace_and_love moolah_market shibe_swag undieguys urban_graff kravshop got_doge mean_elephant the_dogedoor_store';


setInterval( function() {
    var term = $("input").val();
    $("#results").html(obj[term]);
}, 100);

Here's the fiddle: http://jsfiddle.net/ZL7EQ/

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

Comments

1

The only way I know how to solve this is using eval:

$("#results").html(eval(term))

But you really shouldn't want to do that. :)

2 Comments

@rla4, @william44isme using eval() on a user input is a big security issue!!
Just edited my answer and added a link explaining why not. Just keep in mind that it has many pitfalls and use it wisely
1

Use a dictionary.

// store strings in an object so you can look them up by key
var dict = {
    clothing: 'dogeshop ...',
    anotherKey: '...'
};

// update the result when the input changes
$('input').on('change', function() {
    var result = dict[this.value]; // make sure the key exists

    if (result) {
        $('#results').val(result); // update the results
    }
});

1 Comment

Wow! This is fantastic, thanks for optimising the code by removing that interval too.
0

I use to make this mistake myself when working with jQuery.

Try instead:

 $("#results").text(term);

Not sure what you have the interval for. You could just change the #results with a change event.

4 Comments

results.text not html :)
Yeah I tried that but it still doesn't seem to work. jsFiddle
See w3schools.com/jquery/jquery_dom_set.asp , it shows the why and how. I know it works for the code, I took it from my own work. Let me try what you have there. And Also your interval event , look to the next answer for the ON event
@alexmac you are misinterpreting the problem. It's not about setting the contents of the div; the issue is that the string clothing is being printed instead of the contents of the variable clothing.

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.