VIJAYARAJ
VIJAYARAJ

Reputation: 5

How to display multiple object values in alert box using javascript

function myFunction()
{
    alert(document.getElementById("myname").value+','+document.getElementById("myphone")+','+document.getElementById("mycountry")+','+document.getElementById("myemail"));
   
}

Upvotes: 0

Views: 438

Answers (1)

R4ncid
R4ncid

Reputation: 7129

it seems pretty straightforward to me

I just refactor a bit your function

function myFunction() {
  const message = ['myname', 'myphone', 'mycountry', 'myemail']
    .map(id => document.getElementById(id).value)
    .join(',')
  alert(message);

}
<div>
  <input id="myname" placeholder="myname" />
  <input id="myphone" placeholder="myphone" />
  <input id="mycountry" placeholder="mycountry" />
  <input id="myemail" placeholder="myemail" />
</div>
<div>
<button onclick="myFunction()">Show alert</button>
</div>

Upvotes: 1

Related Questions