2

I would like to create JS code that put next elements from array in form after some action, for example after click.

I want to run this code in console (firebug, firefox F12) while im visiting some website. I created test example of code that should work on stackoverflow but it doesn't, why?

Example - stackoverflow.com:

After clicking in header area (id #question-header), my next element from array should be displayed in search form input (name q).

Code doesnt work when I run it in firefox console.

    var arr = ['foo', 'bar', 'baz'];
var i = 0;

function nextItem() {
    i = i + 1; // increase i by one
    i = i % arr.length; // if we've gone too high, start from `0` again
    return arr[i]; // give us back the item of where we are now
}

window.addEventListener('load', function () {
    document.getElementsByName('q').value = arr[0]; // initial value
    document.getElementById('question-header').addEventListener(
        'click', // we want to listen for a click
        function (e) { // the e here is the event itself
            document.getElementsByName('q').value = nextItem();
        }
    );
});

How I can fix it?

5
  • what kind of element is your #search? Commented Jan 12, 2016 at 21:07
  • @madox2 yea, changed to getElementsByName('q') but still not working. Commented Jan 12, 2016 at 21:16
  • but what type is #search? is it div, span or input? Commented Jan 12, 2016 at 21:17
  • @madox2 q is input, search is form.. but input still not working Commented Jan 12, 2016 at 21:20
  • (side note) A quick one-liner for nextItem could be return arr[++i % arr.length]; Commented Jan 12, 2016 at 21:25

3 Answers 3

4

input elements are self-closing tags therefore textContent does not work. It's value can be accessed by value attribute:

document.getElementById('search').value = nextItem();

UPDATE:

According to question update I think the main problem in your code is that you attach listener on page load. You don't need to do it when you paste code to browser console. Here is the working code on SO:

var arr = ['foo', 'bar', 'baz'];
var i = 0;

function nextItem() {
    i = i + 1;
    i = i % arr.length;
    return arr[i];
}

// getElementsByName returns array of elements so access it by index
document.getElementsByName('q')[0].value = arr[0];

// because header contains inner link, add listener on it and prevent it from navigating
var header = document.getElementById('question-header');
var headerLink = header.getElementsByTagName('a')[0];

// when you pass code directly to console, page is (in most cases) already loaded
// you don't need to add listnener to 'load' event
headerLink.addEventListener(
    'click',
    function (e) {
        document.getElementsByName('q')[0].value = nextItem();
        e.preventDefault(); // prevent link to navigate
    }
);
Sign up to request clarification or add additional context in comments.

3 Comments

Code still not working. I just updated my code in post. Changed to: document.getElementsByName('q').value = arr[0];
I only get undefined. Im runnig this code in firebug console while I'm on stackoverflow.
Now it change search value at beggining, but when I click question-header nothing happens
0
document.getElementsByName('q').value = arr[0]; // initial value

This is wrong (should give an error), because document.getElementsByName('q') is a nodeList (similar to array) that has no value property. Most probably you have only one element name q, so you can use [0] to get the first (and only) one.

document.getElementsByName('q')[0].value = arr[0];
// ...
document.getElementsByName('q')[0].value = nextItem();

And I'd suggest storing this element in a variable so that you don't load it every time you need it but only once.

var el = document.getElementsByName('q')[0];
// ...
el.value = arr[0];// or nextItem();

2 Comments

Still it's not working. I want to run this code in console during visit at stackoverflow. I click on header, and stackoverflow search input should give me a array element.
@MichałLipa Then look at the update of madox2's answer.
0

This is working:

HTML

<div id="header">
</div>
<form>
<input type="text" id="search" size="16" maxlength="16" /> 
</form>

JS

var arr = ['foo', 'bar', 'baz'];
var i = 0;
var search = document.getElementById('search');
function nextItem() {
    i = i + 1; // increase i by one
    i = i % arr.length; // if we've gone too high, start from `0` again
    return arr[i]; // give us back the item of where we are now
}

window.addEventListener('load', function () {
    search.textContent = arr[0]; // initial value
    document.getElementById('header').addEventListener(
        'click', // we want to listen for a click
        function (e) { // the e here is the event itself
            search.value = nextItem();
        }
    );
});

CSS

#header{
  width:500px;
  background-color:#444444;
  color:#fff;
  text-align:center;
  padding-top:40px;
  padding-bottom:40px;
}

FIDDLE

1 Comment

Yea, its working in this example but I would like to run this code in console during my visit at stackoverflow. I working on stackoverflow elements (want to click on header and get my array element in search input).

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.