0

Does anyone know how to use a global declared variable inside the following function?

Global value:

window.waarde = document.getElementById("id_x").value;

Function:

$(function () {
  $('input[value="This is where the variable should be"]')
    .filter(':visible:first')
    .prop('checked', true);
});

I've looked everywhere but can't find an answer.

3
  • have you tried using window.waarde or just waarde? Commented Jan 20, 2020 at 12:24
  • What is the problem with this code? Commented Jan 20, 2020 at 12:27
  • Well, at first i'm a total js noob :-) It seems the code didn't work because of the straight '' instead of these `` . Now it does work with `` Commented Jan 20, 2020 at 12:37

3 Answers 3

3

Use it like a normal variable. Accessing a property from a object.

window.waarde = document.getElementById("id_x").value; .

$(function () { 
  $(`input[value="${window.waarde}"]`)
      .filter(':visible:first')
      .prop('checked', true)
});
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, this worked! Did you write $(input[value="${window.waarde}"]) for a reason with `` instead of '' ? When I used the straight ones it didn't work, but with these `` it does work!
0

In general once you got a variable in a script like this, it will be available also within your functions:

myvar = 10; // Same as window.myvar = 10;

function readMyVar() {
  console.log( myvar ); // Will output 10
}

in your case to access the variable inside your function:

waarde = document.getElementById("id_x").value;

$(function () {
  $(`input[value="${waarde}"]`)
    .filter(':visible:first')
    .prop('checked', true);
});

or just:

$(function () {
  $('input[value="' + waarde + '"')
    .filter(':visible:first')
    .prop('checked', true);
});

3 Comments

Thanks! The first one doesnt work, the second one does work. What exactly is the difference in " or ' ?
Yes, it's because of the missed backticks, around the ${}, but the simplest solution is the second one :) If you want to read more about Javascript String Templates: developer.mozilla.org/en/docs/Web/JavaScript/Reference/…. Now I've corrected the error.
The single quote char ( ' ) is used to define the selector, the double quote to define the value of the value attribute. We need to interrupt insert the waard value which otherwise would have been interpreted just as text and the selector would look like this: 'input[value="waarde"]'.
0

@Sybrentjuh, As you have get the desired answer, still I would like to share my answer for the same. I hope that will be helpful.

var waarde = document.getElementById("id_x").value;

$(function () {
  $('input[type=checkbox][value="'+waarde+'"]')
    .filter(':visible:first')
    .prop('checked', true);
});

function filterCheckboxList() {
  waarde = document.getElementById("id_x").value;
  console.log('Checking...', waarde);
  
  // First uncheck all
  $('input[type=checkbox]').prop('checked', false);
  
  // Check only those value matches
  $('input[type=checkbox][value="'+waarde+'"]')
      .filter(':visible:first')
      .prop('checked', true);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" value="10" id="id_x" onChange="filterCheckboxList()" />

<input type="checkbox" value="10" /> Checkbox 1
<input type="checkbox" value="10" /> Checkbox 2
<input type="checkbox" value="15" /> Checkbox 3
<input type="checkbox" value="12" /> Checkbox 4
<input type="checkbox" value="12" /> Checkbox 5

Thanks, Jignesh Raval

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.