-3

I have two pages, so i'm sending data from one page to another page. and getting the data in another page. I'm assigning the value to to input field using document.getElementsById but i want to do the by using document.getElementsByClassName. When i'm trying to push the value using class name it's not working. How can i do that using class name, if i wan't to get the same value in two or three input fields

page1.html

 <form action="form.html" method="GET" class="text-center">
  <input type="text" name="job_id" value="NC-004" style="display: none" />                       
  <input type="submit" value="Apply" class="btn btn-primary" />
 </form>

I'm getting the value in next page using window.location.search

<input type="text" class=" form-control" id=""write value="" />
 <script>
    var n = window.location.search.slice(8);
    console.log(n);
    document.getElementsById("write").value = n;// want to update the value using document.getElementsByClassName
</script>
Sai Manoj
  • 3,809
  • 1
  • 14
  • 35
  • `document.getElementsByClassName` returns an `Array`. You have to loop over the elements and update each element's value. – nicholaswmin Dec 04 '18 at 13:56
  • 1
    @NicholasKyriakides — It's a live node list, not an array. – Quentin Dec 04 '18 at 13:57
  • @True; Sorry, `Array.from(document.getElementsByClassName('.class'))`, will return an Array. – nicholaswmin Dec 04 '18 at 13:57
  • 1
    @Sai Regardless of this being a dupe, the question title is completely misleading. When writing the title to your question, please make sure you're asking about the actual problem, not something unrelated that you in fact know how to do. –  Dec 04 '18 at 13:59
  • @ChrisG edited. Thanks – Sai Manoj Dec 04 '18 at 14:03

2 Answers2

3

You need to do something like this

document.getElementsByClassName('className')[index of the class]
2

While getElementsByClassName return an array so you have to use it like this

document.getElementsByClassName("your_class_name")[0].value = n;// want to update the value using   document.getElementsByClassName

And this is because ID is identical in the page and class may be use by more than one item.

getElementById (return one object) getElementsByClassName (return array of objects with that class)

Here 0 if you have only one element to work with else you have to define index of element you want to work with depending on it is index in the array

Osama
  • 2,912
  • 1
  • 12
  • 15