Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble setting the HTML of a variable in JQuery

Tags:

What I've done is loaded some HTML from a file and I am attempting to modify some elements within that HTML.

The initialization looks like this:

var id = player_info["ID"];
$("#main_container").append(
    $("<div />").attr({class: "player_container", id: "player_" + id}).css("display", "none")
);

// Add all information to the player container
var player_container = $("#player_" + id);
player_container.load("player_layout.html");

With player_layout.html looking like this:

<div class="player_name">

</div>
<div class="player_chips">
    Chips:
    <br/>
    <span class='bidding'></span>/<span class='chips'></span>
</div>
<div class="player_stats">
    Wins / Losses
    <br/>
    <span class="wins"></span>/<span class="losses"></span>(<span class="total_games"></span>)
    <br/><br/>
    Chips Won / Chips Lost
    <br/>
    <span class="chips_won"></span>/<span class="chips_lost"></span>
</div>
<button class="player_won">Player Has Won</button>

I then want to modify some of the elements, specifically classes. An example of the way I was initially doing this is:

player_container.find(".player_name").text(player_info['username']);

This wasn't working so I then tried to switch find with children and text with html but that didn't seem to work. I then tried this:

$('> .player_name', player_container).html(player_info['username']);

but that also didn't work. I understand that I can use DOM to grab the childNodes and compare the class names but there are a lot of classes that need modifying and I'd also like to know if this is possible in JQuery. Thanks in advance for any help.

like image 819
SnowPie Avatar asked Dec 30 '16 09:12

SnowPie


People also ask

How do you declare a variable in jQuery?

Declare a variable using the var keyword. Initialize using the = symbol. Example: var hText = "This is just some text." - Click on the heading to see the text displayed in the <p> elements.

What does the $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.

What is jQuery in HTML?

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is to make it much easier to use JavaScript on your website. jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

What are jQuery variables?

JavaScript (jQuery) variable will have only two scopes. Global Variables − A global variable has global scope which means it is defined everywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined.


1 Answers

You need to use complete callback method of .load()

var player_container = $("#player_" + id);
player_container.load("player_layout.html",  function(){
    player_container.find(".player_name").text(player_info['username']);
});
like image 146
Satpal Avatar answered Sep 22 '22 10:09

Satpal