Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery using "subquery"

I am moderately certain the answer has already been answered, my issue is I don't know what to even ask.

I am trying to dynamicly add already existing HTML divs by cloning them and changing the values, and I ran into an issue

HMTL :

    <div class="worldRow">
        <div class="worldProperties">Name</div>
        <div class="worldProperties worldName">World name</div>
        <div class="worldProperties">Population</div>
        <div class="worldProperties worldPopulation">50</div>
        <div class="worldProperties">Occupency</div>
        <div class="worldProperties worldOccupency">45%</div>
    </div>

Javascript :

function addWorld(data){
    var row = $('#fakeWorldList .worldRow').clone();
    row = $(row).attr('id',data.worldId);
    $(row).('.worldName').value('this will never work, halp.');
}

In my javascript, on the last row of my function, I am trying to set the div with the class "worldName" a new value, but I simply cannot figure how.

If anyone could point me in the right direction, it would be much appreciated.

Cheers

like image 385
Munsta0 Avatar asked May 01 '13 03:05

Munsta0


2 Answers

You need to use .find() to search for a child element

$(row).find('.worldName').text('this will never work, halp.');

Ex:

function addWorld(data){
    var row = $('#fakeWorldList .worldRow:first').clone();
    row = $(row).attr('id',data.worldId);
    $(row).find('.worldName').text('this will never work, halp.');
}
like image 58
Arun P Johny Avatar answered Sep 29 '22 15:09

Arun P Johny


.value is for input elements, you need to do

$(row).find('.worldName').html('this will never work, halp.');
like image 34
dave Avatar answered Sep 29 '22 15:09

dave