0

I have some div's dynamically loaded with javascript and I want to get the text from those div's and append them into an existing div.

So, here's my code :

var firstoption  = jQuery( ".myo-poll-bar.firstoption"  ).text();
var secondoption = jQuery( ".myo-poll-bar.secondoption" ).text();

jQuery( ".votefilters span a:first" ).html( firstoption  );
jQuery( ".votefilters span a:last"  ).html( secondoption );

I know that there is an .append() method but I don't know how can I use it in this code.

Any suggestions are welcome.

Thank you !

5 Answers 5

1
var firstoption = jQuery( ".myo-poll-bar.firstoption" ).text();
var secondoption = jQuery( ".myo-poll-bar.secondoption" ).text();

var curr_text = jQuery( ".votefilters span a:first" ).text();
var curr_text2 = jQuery( ".votefilters span a:last" ).text();

jQuery( ".votefilters span a:first" ).html( curr_text  + firstoption );
jQuery( ".votefilters span a:last" ).html( curr_text2  + secondoption );

You can assign the object result to some variables so you don't have to traverse the dom twice to get the same object

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

Comments

1

Using multi-condition in one selector is not a good practice.

For higher performance, if .firstoption is enough, do not use .myo-poll-bar.firstoption

and split

jQuery( ".votefilters span a:first" )

to

jQuery(".votefilters").find("span").find("a:first")

All:

var firstoption  = jQuery( ".myo-poll-bar.firstoption"  ).text();
var secondoption = jQuery( ".myo-poll-bar.secondoption" ).text();

jQuery(".votefilters").find("span").find("a:first").append(firstoption);
jQuery(".votefilters").find("span").find("a:last" ).append(secondoption); 

1 Comment

Why is a multi-condition selector "bad practice"?
0
$(firstoption).appendTo('.votefilters span a:first');
$(secondoption).appendTo('.votefilters span a:last');

Comments

0

Try this.

$( ".votefilters span a:first" ).append( document.createTextNode( $( ".myo-poll-bar.firstoption" ).text() ) );
$( ".votefilters span a:last" ).append( document.createTextNode( $( ".myo-poll-bar.secondoption" ).text() ) );

Comments

0

Something like this should suffice:

var $first = jQuery( ".votefilters span a:first" );
var $last = jQuery( ".votefilters span a:last" );

$first.text(  $first.text() + firstoption );
$last.text( $last.text() + secondoption );

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.