1

what i Need

  • i need to pass url variable from html to javascript file.

html code

    <script>
     var p = "{{ page_param.asy_request | replace('&','&')}}";
     p = p.replace(/\&/g, "&");
     p = "{{ DomainDetect() }}/ajax?for=event_listing&ajax=1" + p;

   </script>

 <input type="hidden" name="mobilepage" id="mobilepage" value="'+p+'"/>

Accesing p variable in javascript code

   var mobile=document.getElementById("mobilepage").value;
   console.log(mobile);

Error

Uncaught TypeError: Cannot read property 'value' of null
  • i need url should be passed from html file to js file.

  • i have used hidden element for that.

  • any suggestion are most welcome.

0

3 Answers 3

1

Your html element hasn't id. Hence you can't select using the document.getElementById. If you want to fix this, you have to add this id, like below:

<input type="hidden" name="mobilepage" id="mobilepage" value=""/>

If you don't want to add an id and you want to leave this element as is, the you should use another method for selecting your element, document.getElementByName.

Below I have added a code snippet to see that it works.

alert(document.getElementById("mobilepage").value);
<input type="hidden" name="mobilepage" id="mobilepage" value="3"/>

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

5 Comments

@user3785613 have you added the id and it didn't?
updated <script> var p = "{{ page_param.asy_request | replace('&','&')}}"; p = p.replace(/\&/g, "&"); p = "{{ DomainDetect() }}/ajax?for=event_listing&ajax=1" + p; console.log(p); </script> <input type="hidden" name="mobilepage" id="mobilepage" value='"+p+"'/>
js file var mobile=document.getElementById("mobilepage").value; console.log(mobile);
@user3785613 I think that your problem relies on the way you have defined the value.
alert(document.getElementById("mobilepage") return null
0

You're trying to get an element by ID, when you should be getting that element by name. Do:

 var mobile=document.getElementByName("mobilepage").value;

Either that, or change the name to be an ID on the HTML element:

<input type="hidden" id="mobilepage" value=""/>

3 Comments

still the same problem Uncaught TypeError: Cannot read property 'value' of null
updated <input type="hidden" name="mobilepage" id="mobilepage" value="'+p+'"/>
@user3785613 If it doesn't work then you have a mismatch between your scripts and the DOM elements, because the element is clearly not available, thus returning undefined.
0

Add id attribute to your input.

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.