1
function searchString(user_input, search) {
    var user_string = document.getElementById('user_input');
    var search_string = document.getElementById('search');

    document.write(search_string.value);
}

The document would'nt print the value.I am new to javascript and finding it hard to figure out why? I also tried this:

function searchString(user_input, search) {
    var user_string = document.getElementById('user_input');
    var search_string = document.getElementById('search').value;

    document.write(search_string);
}

But no results. I am a noob, please help?

Gorgon_Union
  • 535
  • 2
  • 8
  • 22
smartnerd
  • 85
  • 4
  • 1
    `document.write` has to be used within the document. The Elements must exist before the JavaScript or they're undefined. You shouldn't use document.write dynamically. – StackSlave May 01 '17 at 01:49
  • Are you getting any errors in your console? –  May 01 '17 at 01:56
  • 3
    Please see [Why is `document.write` considered a 'bad practice'](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). –  May 01 '17 at 01:57
  • Thanks @PHPglue. – smartnerd May 01 '17 at 04:10

1 Answers1

-1

Don't use document.write. Add jQuery to your site instead, then you'd simply do something like:

$('#search').val(search_string);

And you'd get the values using:

var test = $('#searchBox').val();

If you want to do something professional grade I recommend getting into Vue.js as early as possible, it's essentially built to tackle this specific type of problem.

Doing "raw" javascript is sometimes a good thing but don't make things difficult for you. When you have to edit the DOM, use jQuery. If you want to build a website that is highly responsive to various data on your website, use Vue.js. Don't use raw javascript if you're a noob.

Simon Hyll
  • 2,342
  • 3
  • 19
  • 33
  • @A.Lau Note how I also mentioned Vue.js, not just jQuery. Also note how the jQuery code doesn't use plugins. Also note how it is genuinely a good idea for this person to consider using either Vue or jQuery since if you have questions like this you're probably not good enough to handle making things from scratch in pure javascript. Sometimes the answer IS more jquery. – Simon Hyll May 01 '17 at 03:15
  • except this isn't the case. Your line literally does nothing different to `document.getElementById('search').value;` – A. L May 01 '17 at 03:18
  • @A.Lau you seem to forget the part about writing the value as well... And about how much better it is for this person to start using jQuery or Vue... – Simon Hyll May 01 '17 at 03:24
  • 1
    Learning jQuery is one thing. Learning vue.js because of this one insignificant problem shows how terrible you are at giving advice. Employers will look first at basic javascript programming before any additional libraries, because they may need you to extend their in-house code or to create their own. Relying everything on a framework/library without understanding vanilla code almost makes you a script kiddie. And why not tell him to learn Angular, Ember or React? – A. L May 01 '17 at 03:31
  • oh and as for writing out the value (from your example) `document.getElementById("searchBox").value = document.getElementById('search').value;` – A. L May 01 '17 at 03:32
  • That's why I brought up jQuery first. Vue.js is amazing and he would do good to get into it asap, but I brought up jQuery first because it's the easiest fix to his problem. I also brought up the reason you might use Vue or jQuery. jQuery for DOM manipulation and Vue for displaying data. I don't recommend those other frameworks because I haven't used them enough to recommend them, and I know Vue and jQuery are compatible with a lot of project, I'm combining Vue, jQuery, Cordova currently with a Node.js Express backend server and it's working great so that's why I recommend it. – Simon Hyll May 01 '17 at 03:41
  • Also, it depends entirely on what your employer wants you for. If you're hired as a javascript programmer of course they want javascript skills, but if you're hired as a Vue programmer or jQuery programmer they don't care as much. If you're on Stackoverflow asking these questions however what your employer is going to think is not your first concern, learning javascript is, and the best way to learn something is having fun while doing it, and great results fast is the best, most successfull way of getting someone to enjoy programming, hence recommending easy frameworks for them to use. – Simon Hyll May 01 '17 at 03:43
  • like I said, jQuery literally does nothing better than what vanilla js couldn't do. If you wanted to recommend jQuery (even though this question is highly unsuited for it). You could post it as a recommendation below the actual answer (which should be in vanilla js as there is no use of jquery or the jquery tag). – A. L May 01 '17 at 03:49
  • Of course it doesn't do anything better, no javascript framework does anything better than vanilla javascript SINCE THEY'RE ALL JAVASCRIPT FRAMEWORKS. Your logic would mean there's never a use for any frameworks because you can always just recreate the functionality the frameworks offer. The problem is that in the real world time is money and using a framework will not only save you a lot of headaches but also time. Employers care more about saving money than they care about the programmer not using a framework to save time. It's like saying C++ is a waste when you have Assembly... – Simon Hyll May 01 '17 at 03:55
  • no there are definitely uses for plugins, as you don't have to reinvent the wheel. Something like `.hide()` and `.show()` are very good examples of why using jQuery is good. But your answer does not demonstrate any simplicity and increases complexity by introducing a plugin when the user has no established the basics yet. – A. L May 01 '17 at 03:57