Manu
Manu

Reputation: 3247

how to convert object to string in javascript?

Below i have pasted my piece of javascript code here

1)

  function showCustomerName(dropdown) {
  var selectedCustomer = dropdown.options[dropdown.selectedIndex].value;
  var currentCustomer = document.getElementById('currentCustomer');
  alert(selectedCustomer); 

  var context = document.forms[0].context.value;
  document.forms[0].mode.value = "UPDATE";
  document.forms[0].custName.value = selectedCustomer ;
  document.forms[0].action=context+"/updateReportDetail.do";
  document.forms[0].method="POST";
  document.forms[0].submit();
}

2)

 function showCustomerID(dropdown) {
  var currentCustomer = document.getElementById('currentCustomer');
  var selectedCustomerID = dropdown.options[dropdown.selectedIndex].value;
  alert(selectedCustomerID);   

  var context = document.forms[0].context.value;
  document.forms[0].mode.value = "UPDATE";
  document.forms[0].custName.value = currentCustomer;
  document.forms[0].custID.value = selectedCustomerID ;
  document.forms[0].action=context+"/updateReportDetail.do";
  document.forms[0].method="POST";
  document.forms[0].submit();
}


<select id="currentCustomer" onchange="showCustomerName(this)">
 <c:forEach var="Customer" items="${listCustomer}" >
   <option value="<c:out value="${Customer}" />"><c:out value="${Customer}" />
   </option>
  </c:forEach>
 </select>

<select id="currentCustomerID" onchange="showCustomerID(this)">
 <c:forEach var="CustomerID" items="${listCustomerID}" >
   <option value="<c:out value="${CustomerID}" />"><c:out value="${CustomerID}" />
   </option>
  </c:forEach>
 </select>

the above code is working fine. The thing is i need to retrieving value from "showCustomerName()" to "showCustomerID()" script .

In "showCustomerID()" script method i am retrieving "showCustomerName()" methods value as object

var currentCustomer = document.getElementById('currentCustomer');

how to retrieve it as a value? i dont want to retrieve as object

please help me guys.

Upvotes: 1

Views: 5359

Answers (6)

Shamit Bagchi
Shamit Bagchi

Reputation: 23

Does:

var currentCustomer = document.getElementById('currentCustomer').value;

return a string for a form type {input type="text" id="currentCustomer"}

Upvotes: 0

James Curran
James Curran

Reputation: 103485

getElementById is going to return the element as an object. There's no way around that.

What you need to do is get the value outof that:

var currentCustomerEle = document.getElementById('currentCustomer'); 
var currentCustomer = currentCustomerEle.value;

or you could do it in one line:

var currentCustomer = document.getElementById('currentCustomer').value;

Upvotes: 1

Xavier Combelle
Xavier Combelle

Reputation: 11195

try this

var currentCustomer = document.getElementById('currentCustomer').value;

Upvotes: 1

Mechlar
Mechlar

Reputation: 4974

var currentCustomer = document.getElementById('currentCustomer').value;

Getting an element is an object. You can then target the object to get its value.

Upvotes: 2

CaffGeek
CaffGeek

Reputation: 22054

Just need to grab the value

var currentCustomer = document.getElementById('currentCustomer').value;
alert(currentCustomer);

Though you should make sure it exsts

var currentCustomerElement = document.getElementById('currentCustomer');
var currentCustomer = (currentCustomerElement) ? currentCustomerElement.value : "not found";

Or am I misunderstanding what you are trying to retrieve?

Upvotes: 4

Colin Hebert
Colin Hebert

Reputation: 93157

Did you tried this :

var currentCustomer = document.getElementById('currentCustomer').value;

Upvotes: 2

Related Questions