Wednesday, January 11, 2012

jQuery Mobile: Add HTML elements using Javascript code

It's mobile version of the last exercise "Add HTML elements using Javascript code", to make it mobile look and feel using jQuery Mobile.

jQuery Mobile: Add HTML elements using Javascript code

<!doctype html>
<head>
<title>Mobile-Web-App: Add HTML elements using jQuery Mobile</title>
<meta charset="utf-8">
<meta name = "viewport" content = "width = device-width, height = device-height" />

<link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>

<script>
function myOnLoad(){
var myButton1 = document.getElementById("Button1");
myButton1.onclick = Button1Listener;
}

function Button1Listener(){
var myText1 = document.getElementById("Text1");
var myInput1 = myText1.value;

var newItem = document.createElement("li");
newItem.innerHTML = myInput1;
var myItems = document.getElementById("myitems");
myItems.appendChild(newItem);

}

window.onload = myOnLoad;

</script>


</head>
<body>

<div data-role="page" id="home">
<div data-role="header">
<h1>jQuery Mobile: Add HTML elements using Javascript code</h1>
</div>

<div data-role="content">
<p>Enter new item</p>
<form>
<input type="text" id="Text1">
<input type="button" id="Button1" value="Add">
</form>
<ul id="myitems">
</ul>
</div>

<div data-role="footer">
<h4><a href="http://mobile-web-app.blogspot.com/">http://mobile-web-app.blogspot.com/</a></h4>
</div>

</div>

</body>
</html>


In this example, <ul> is in standard html element. It will be modified to jQuery Mobile style "listview" in next post: jQuery Mobile: listview with data-filter.

No comments:

Post a Comment