Javascript Object and HTML form element mapping| Class in Javascript

Published: 28 April 2020
on channel: TravelTales 360
56
5

In this video, I have explained how to do javascript object mapping with HTML form elements and vice versa

The source code used in this tutorial is pasted below.

(lt) is here meant start tag character which is not allowed to be pasted in youtube
(gt) is here meant end tag character which is not allowed to be pasted in youtube

(lt)!-- HTML forms and Javascript Object Mapping --(gt)
(lt)html(gt)
(lt)head(gt)
(lt)title(gt)My Javascript Excercise(lt)/title(gt)
(lt)script(gt)

function Student()
{
this.roll_no="1";
this.first_name="Kuntal";
this.last_name="Chakraborty";
this.dept="IT";
}

var studentObject=new Student();

function JSObjectToUI()
{
document.getElementById("roll_noId").value= studentObject.roll_no;
document.getElementById("first_nameId").value= studentObject.first_name;
document.getElementById("last_nameId").value= studentObject.last_name;
document.getElementById("deptId").value= studentObject.dept;
}

function UIToJSObject()
{
var newstudentObject=new Student();
newstudentObject.roll_no=document.getElementById("n_roll_noId").value ;
newstudentObject.first_name=document.getElementById("n_first_nameId").value ;
newstudentObject.last_name=document.getElementById("n_last_nameId").value ;
newstudentObject.dept=document.getElementById("n_deptId").value ;

alert('New Student Name :'+newstudentObject.first_name+' '+newstudentObject.last_name+' Roll No: '+newstudentObject.roll_no+' Dept:'+newstudentObject.dept);
}
(lt)/script(gt)
(lt)/head(gt)
(lt)body onload="JSObjectToUI();"(gt)

(lt)form name="studentForm"(gt)

Roll No(lt)input type="text" name="roll_no" id="roll_noId"(gt)(lt)br(gt)
First Name(lt)input type="text" name="first_name" id="first_nameId"(gt)(lt)br(gt)
Last Name(lt)input type="text" name="last_name" id="last_nameId"(gt)(lt)br(gt)
Dept(lt)input type="text" name="dept" id="deptId"(gt)(lt)br(gt)

(lt)/form(gt)

(lt)form name="newStudentForm"(gt)

Roll No(lt)input type="text" name="n_roll_no" id="n_roll_noId"(gt)(lt)br(gt)
First Name(lt)input type="text" name="n_first_name" id="n_first_nameId"(gt)(lt)br(gt)
Last Name(lt)input type="text" name="n_last_name" id="n_last_nameId"(gt)(lt)br(gt)
Dept(lt)input type="text" name="n_dept" id="n_deptId"(gt)(lt)br(gt)

(lt)input type="button" value="Generate" onClick="UIToJSObject();"(gt)

(lt)/form(gt)

(lt)script(gt)

(lt)/script(gt)
(lt)/body(gt)
(lt)/html(gt)


On this page of the site you can watch the video online Javascript Object and HTML form element mapping| Class in Javascript with a duration of hours minute second in good quality, which was uploaded by the user TravelTales 360 28 April 2020, share the link with friends and acquaintances, this video has already been watched 56 times on youtube and it was liked by 5 viewers. Enjoy your viewing!