0

I am sorry that I found what I did wrongly.. just typal mistake between the html file and txt file

First of all, I apologies to my poor English.

I am trying to get the html code inside a div in the external txt file dynamically through ajax by jQuery. However, I cannot get the html code in specific div as I expected. Here comes my code

.html file

<div id="html_div_id_A">
  <div class="html_div_class">
  </div>  
</div>

<div id="html_div_id_B">
  <div class="html_div_class"> 
  </div>  
</div>

abc.txt file

<div id = "txt_div_id_A">
  <div class="txt_div_class">
        some content A
  </div>  
</div>

<div id = "txt_div_id_B">
  <div class="txt_div_class">
        some content B
  </div>  
</div>

.js file

function func(arg)
{
    var htmlDivId = $(arg).attr("id").toString()// html_div_id_A or html_div_id_B, decided in run time
    var txtDivId = htmlDivId == "html_div_id_A" ? "#txt_div_id_A" : "#txt_div_id_B";// txt_div_id_A or txt_div_id_B, decided in run time

    // What I could do now
    $(htmlDivId + " > .html_div_class").load("abc.txt #txt_id_B div");

    // What I expected to do
    $(htmlDivId + " > .html_div_class").load("abc.txt " + txtDivId + " div");
}

I have tried the solution that suggest in here (Use Jquery Selectors on $.AJAX loaded HTML?) but it does not work. Are there any way to solve this problem?

Community
  • 1
  • 1
Biboo Chung
  • 121
  • 1
  • 2
  • 13

1 Answers1

0

Try

function func(arg)
{
    var htmlDivId = $(arg).attr("id");

    //You need to assign the string value of the target id
    var txtDivId = htmlDivId == "html_div_id_A" ? 'txt_div_id_A' : 'txt_div_id_B';// txt_div_id_A or txt_div_id_B, decided in run time

    // What I expected to do
    // need to prepend # to target id
    $('#' + htmlDivId + " > .html_div_class").load("abc.txt #" + txtDivId + " div");
}

Demo: Plunker

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thank you~ However, I did it correctly in my real code. Thank you for your contribution and I discovered my mistake already – Biboo Chung Jun 18 '13 at 06:09