0

I am creating a mobile app. I have a javascript file language.js having the following code

var lang=new Array(); 
lang[0]="English";      
lang[1]="Chinese";
lang[2]="Korean";

I want to create checkbox input based on the array in html like :

<div data-role="fieldcontain" class="ui-hide-label">
<input type="checkbox" name="English" value="0" onclick="setvalue(this.value)">English<br><input type="checkbox" name="Chinese" value="1" onclick="setvalue(this.value)">Chinese<br><input type="checkbox" name="Korean" value="2" onclick="setvalue(this.value)">Korean</div>

How do i insert the array from language.js in HTML? Thanks!

1
  • Have you considered using a solution like jQuery templates? They're ideal for dynamic construction of html from javascript-defined data. Seriously, they've rocked my world. net.tutsplus.com/tutorials/javascript-ajax/… Commented Aug 9, 2013 at 19:54

1 Answer 1

3

You'd probably want something like this:

function buildLanguages() {
    var div = document.getElementById("langDiv");

    var html = "";
    for (var i=0;i<lang.length;i++) {
        html += "<input type='checkbox' name='" + lang[i] + "' value='" + i + "' onClick=\"setValue(this.value);\">" + lang[i] + "<br>";
    }

    div.innerHTML = html;
}

And your html:

<div data-role="fieldcontain" class="ui-hide-label" id="langDiv">
</div>

A Fiddle for the Demo: http://jsfiddle.net/4qb9Y/

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

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.