1

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.

1
  • Can't you call currentCustomer.toString()? Commented Aug 24, 2010 at 16:48

6 Answers 6

4

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?

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

Comments

2

Did you tried this :

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

Comments

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

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

Comments

1

try this

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

Comments

1

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;

Comments

0

Does:

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

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

1 Comment

Your answer doesn't really add anything to the many anwers already given to the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.