Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
IS there a way to access data-binding in span tag and store it in a variable in the script tag? knockoutjs

oi

<table class="table table-striped table-sm small" >
<thead class="bg-dark text-light">
    <tr>
        <th scope="col">Id</th>
        <th scope="col">Name</th>
        <th scope="col">Nationality</th>
        <th scope="col" class="text-right"></th>
    </tr>
</thead>
<tbody data-bind="foreach: records">
    <tr>
        <td class="align-middle" data-bind="text:DriverId"></td>
        <td class="align-middle" data-bind="text:Name"></td>
        <td class="align-middle">  
            <span id = "nation" data-bind="text:Nationality" class="float-left"> </span>
            
            <img  id="flagicon" src="" alt="" class="flag float-right float-center" />
            <script>
                
                
                var alpha_2_code = "pt";
                
               /* for(let i=0; i < countrycodesobj.length ; i++){
                    if(countrycodes[i].Nationality === (data-bind = "text:Nationality"))
                        alpha_2_code = countrycodes[i].alpha_2_code;
                }*/
                
                var myPath = "https://countryflagsapi.com/png/" + alpha_2_code;
                document.getElementById("flagicon").src= myPath;
            </script>

        </td>
       
    </tr>
</tbody>

I want the information > data-bind="text:Nationality" < stored in a variable in script tag. Im getting the information from an api and its being handled by knockoutjs. My knockoutjs knowledge is scarce and i dont know if i can access the information data-bind="text:Nationality" in the script tags. Any solution whether in jquery , knockouts or javascript would be perfect. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you could make use of a simple function that you define before you applyBindings with knockout and after your countrycodesobj is defined, sth like:

function getFlagByNationality(n) {
    var alpha_2_code = "pt";
                
    for(let i=0; i < countrycodesobj.length ; i++){
      if(countrycodes[i].Nationality == n ){
         alpha_2_code = countrycodes[i].alpha_2_code;
      }
    }
    return "https://countryflagsapi.com/png/"+alpha_2_code;
}

you can then use this in data-bind attribute:

<img src="" data-bind="attr:{src: getFlagByNationality(Nationality)}"  />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!