0

I'm having a difficult time finding out why this code doesn't work. Specifically, why I can't span the variable, and then add to it a class? Of course, I succeed in adding the class if it's just text, and not a variable.:

var fruit = 'bananas';
var paragraph = $('<div></div>');

paragraph.html('I like ' + '<span class="features">'fruit'</span>');
$(".features").css("color","yellow");
3
  • 2
    How does it not work? What is happening? What should be happening? Are there any errors? Commented Oct 21, 2018 at 15:46
  • Looks like a syntax error, needs a + on either side of fruit Commented Oct 21, 2018 at 15:49
  • @RobinZigmond You got it. I can't believe that was all I was missing. Hours and hours and hours spent on THAT. Thanks again. Commented Oct 21, 2018 at 15:55

1 Answer 1

3

A few things I can see that is wrong with your code.

// fruit var is fine
var fruit = 'bananas';

// originally you had html markup in here, but you have to use html selectors.
var paragraph = $('div');

// you were close here but you didnt have plus symbols either side of your fruit var
paragraph.html('I like '+'<span class="features">'+fruit+'</span>');

// add the colour to your .features span - this is fine
$(".features").css("color","yellow");

See working fiddle here. https://jsfiddle.net/joshmoto/k0vrs8ne/

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

1 Comment

That was exactly it! Thank you! Those blasted plus symbols... I appreciate the link to the jsfiddle!.

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.