admin管理员组

文章数量:1302275

I am pulling a cookie value into a hidden form field. If there is a name I know how to do it but when there is not I cannot seem to figure it out. I cannot change the form by adding anything to the input field but need to save a cookie value to that field.

Example:

<p class="form-field anic_source pd-hidden hidden">
<input type="hidden" value="">
</p>

<script>
function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}
  
 document.querySelector("input[name='fieldname']").value = getCookie("anic_source");
</script>

If the form had a name this works great but without one it does nothing. Maybe somehow basing it off of the child of anic_source paragraph but I an still relatively green at js and not sure how that would work.

I am pulling a cookie value into a hidden form field. If there is a name I know how to do it but when there is not I cannot seem to figure it out. I cannot change the form by adding anything to the input field but need to save a cookie value to that field.

Example:

<p class="form-field anic_source pd-hidden hidden">
<input type="hidden" value="">
</p>

<script>
function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}
  
 document.querySelector("input[name='fieldname']").value = getCookie("anic_source");
</script>

If the form had a name this works great but without one it does nothing. Maybe somehow basing it off of the child of anic_source paragraph but I an still relatively green at js and not sure how that would work.

Share asked Feb 10 at 14:22 Brandon OrndorffBrandon Orndorff 33 bronze badges 3
  • If it's the only hidden field you can just search for that/ – 0stone0 Commented Feb 10 at 14:24
  • 1 document.querySelector(".anic_source input[type='hidden']") ...? // This will of course only achieve anything, if the hidden field gets read by some other JS later on. With a normal form submission, this value would not be included in the first place, if the input field doesn't have a name. – C3roe Commented Feb 10 at 14:35
  • That did it thank you. Also works with multiple fields since each one is wrapped in a paragraph with unique classes. There is a name but it changes each time the form is generated so unable to be used so the forms still save. Thank you. – Brandon Orndorff Commented Feb 10 at 14:42
Add a comment  | 

1 Answer 1

Reset to default 0

Alternatively, you could use the id-attribute or any data-* attribute to select the hidden inputfield, e.g. <input type="hidden" data-id="myHiddenField" /> and then select it via document.querySelector( '[data-id="myHiddenField"]' ).

本文标签: javascriptSave Cookie value to hidden input field without name or idStack Overflow