0

I am trying to create elements with dynamic class value, so far unsuccessful. Here's code:

let sender = "alex";
let message = "testMsg";
let time = "15:30";
let msg = $('<span>')
              .append($('<span>', {class : 'timestamp'}, {text: time}))
              .append($('<p>', {class : sender}, {text: message}));

Results in the following:

<span>
    <span id="timestamp"></span>
    <p></p>
</span>

Which is confusing, because I read here that this more "elegant" way allows for variables to be used as dynamical properties. What am I missing here?

Thanks

Solved: Turns out that I tried to give 3 paramethers to .append() function. Read bellow for solutions and explanation.

4
  • There are too many errors in this code, so I guess it's not the real one. Can you build a runnable snippet ? See stackoverflow.com/help/mcve Commented Aug 19, 2016 at 11:56
  • You could use jQuery attr() Commented Aug 19, 2016 at 11:57
  • Oftopic but why are using let instead of var? Commented Aug 19, 2016 at 12:00
  • @DenysSéguret I fixed span and added 'time' variable, only errors I can find. This is the code I am running to produce the above results. Commented Aug 19, 2016 at 12:02

3 Answers 3

2

You were close, Here are small changes which I have done:

  1. Specify tag properly it should be <span> not </span>
  2. Use correct API, you can pass multiple arguments. $('<span />', {class: 'timestamp', text: time })
  3. Define all variables i.e. time was not defined.
  4. should not insert a <p> inside a <span>, Pointed out by @blex

let sender = "alex",
  message = "testMsg",
  time = '15:30';

let msg = $('<span>')
  .append($('<span>', {
    class: 'timestamp',
    text: time
  }))
  .append($('<p>', {
    class: sender,
    text: message
  }));

$('div').append(msg)
.alex{color:green}
.timestamp{color:red}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

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

4 Comments

In fact it shouldn't even be <span/> (which works because the engine is lax but isn't valid) but <span>
@DenysSéguret The jQuery documentation uses the "<span/>" syntax
@blex ok, that's still a terrible practice. It assumes the browsers parses invalid HTML the way you want it to do it.
[The snippet works, but I still don't get any class. Why would that be?] Never mind, fixed it. Thanks guys.
2

try this :

let sender = "alex";
let message = "testMsg";
let time = "15:30";
let msg = $('<span>')
                  .append($('<span>', {class : 'timestamp', text: time}))
                  .append($('<p>', {class : sender, text: message}));

https://jsfiddle.net/MostafaB/hhadxdxe/

1 Comment

Why another identical answer ?
0

You missplaced your second attribute (text)

var $div = $("<div>", {class: 'myClass', text: 'Some text'});
$("body").append($div)
.myClass {
  background-color: black;
  width: 50px;
  height: 50px;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.