1

<input type="hidden" name="email" value="Website_list">
<input type="hidden" name="name" value="[email protected]">
<input type="hidden" name="fname" value="John">

<script type="text/javascript">
 var subscriber_details = {
email : '[email protected]',
name  : 'Website_List',
fname : 'John'
}

</script>

i want to pass the hidden value in Javascript variable in same manner "Subscriber_details" .

So what should i do?

4
  • where is the value attribute ? Commented Apr 23, 2015 at 5:18
  • i dont understand the question.. are you getting these values from the hidden inputs or are you assigning it from your script tag object to each hidden input? Commented Apr 23, 2015 at 5:20
  • I want to pass the value from hidden field to script....!! but i m not able to do so..!!! I want to fetch the value in above manner in script tag\ Commented Apr 23, 2015 at 5:21
  • I m trying to run an API so i want to pass the form value in the API so how should I do that?? & How to run that API? Commented Apr 23, 2015 at 5:23

5 Answers 5

1
<script type="text/javascript">
 var subscriber_details = {
   email : "'"+ document.getElementsByName("email").value +"'",
   name  : "'"+ document.getElementsByName("name").value +"'",
   fname : "'"+ document.getElementsByName("fname").value +"'"
}

</script>
Sign up to request clarification or add additional context in comments.

Comments

1
function(){
  var email = $("input[name='email']").val();
  var name = $("input[name='name']").val();
  var fname = $("input[name='fname']").val();
 var subscriber_details = {
 email : email,
   name  : name,
   fname : fname
};
  console.log(email);
  console.log(name);
  console.log(fname);
}

Comments

0

Assign id and some value to hidden input feilds:

html Code

<input type="hidden" name="email" id="email" value="[email protected]">
<input type="hidden" name="name"  id="name"  value="somename">
<input type="hidden" name="fname" id="fname" value="fname">

Then get it by id:

Javascript:

<script type="text/javascript">
  var email = document.getElementById("email");
  var name = document.getElementById("name");
  var fname = document.getElementById("fname");
  var subscriber_details = {
   email : email
   name  : name,
   fname : fname 
 }
</script>

Comments

0
<script type="text/javascript">
 var subscriber_details = {
   email : $('input[name="email"]').val().trim(),
   name  : $('input[name="name"]').val().trim(),
   fname : $('input[name="fname"]').val().trim()
}
</script>

(if you are using jQuery). Sanjay posted a vanilla js version that would work.

Comments

0

try this DEMO

html:

<input type="hidden" name="email" id="email" value="[email protected]">
<input type="hidden" name="name" id="name" value="Kavin">
<input type="hidden" name="fname" id="fname" value="smk">

script:

$(document).ready(function(){
  var em = $("#email").val();
     var name = $("#name").val();
    var fn = $("#fname").val();
alert(em);alert(name);alert(fn);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.