How to use a variable in a Jquery selector [duplicate] - javascript

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you

Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

Related

jquery adding class to variable inside if, else statement [duplicate]

This question already has answers here:
How to add a class to a given element?
(28 answers)
Closed 2 years ago.
I am trying to add a class to a variable inside one of my if else statements in a jquery function. I am getting an error x.addclass is not a function. I have tried placing the variable in () and without. It makes no difference.
var ideal = document.getElementById( 'ideal_' + id )
var Search = document.getElementById('Search_' + id)
var parent = document.getElementById(+id)
if(ideal){
parent.addClass('myclass')
} else if (Search){
parent.style.background='yellow'
} else {
parent.style.background='none'
}
.addClass() is a jQuery method, you can call that on a jQuery referenced element.
Try using classList.add():
parent.classList.add('myclass');

How can I check the document for an ID and change the style on a completely different class? [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 3 years ago.
I'd like to check the document for a specific ID. If the ID exists, I want to change the margin of a completely different class on the same page. I'm at a loss as to why it's not working. Maybe it's not even possible?
I've tried several different versions of the same type of coding below, so I'm guessing I'm way off base...
var a = document.getElementById('CommunityTabsContainer');
var b = document.getElementsByClassName('librarysearch');
if (a != null) {
b.style.marginTop = "-68px";
} else {
b;
}
You're on the right track. Assuming that CommunityTabsContainer is the id you want to scan for and librarysearch is the class you want to change; you can use document.querySelector to do what you need (documented Here). I would write something like this:
const a = document.querySelector('#CommunityTabsContainer')
const b = document.querySelector('.librarysearch');
if (a !== null) {
b.style.marginTop = '-68px'
}
Note: in your original code, getElementsByClassName returns a collection of elements, not a single element.

javascript - check if string is in a array without case sensitive [duplicate]

This question already has answers here:
How to check if a string array contains one string in JavaScript? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a filter. Where if you input a word that is in the 'blacklist' it will tell something. I've got all the code but have a problem.
JS:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (input == array)
// I will do something.
} else {
// Something too
}
}
I want to make it so that if the input is a item in the array. That the statement is true. But what is the correct way to do this? Because what I'm doing here doesn't work! Also I want to get rid of the case sensitive! So that if the array has hello in it both hello and Hello are detected.
Sorry if this question is asked before. I searched for it but didn't know what keywords to use.
EDIT 1:
I am changing my question a little bit:
I want to check what is in my original question but with some other features.
I also want to check if input has a part of an item in array. So that if the input is hello that helloworld is being detected because is has hello in it. As well as hello or Hello.
Use indexOf:
if (array.indexOf(input) > -1)
It will be -1 if the element is not contained within the array.
This code should work:
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) >= 0)
// I will do something.
} else {
// Something too
}
}
The indexOf Method is member of the array type and returns the index (beginning at 0) of the searched element or -1 if the element was not found.
I think what you are looking for is
input = document.getElementById("input").value;
array = ["1","2","3"];
function filter() {
if (array.indexOf(input) !== -1 )
// I will do something.
} else {
// Something too
}
}

Select dropdownmenu selected item [duplicate]

This question already has answers here:
How can I set the value of a DropDownList using jQuery?
(17 answers)
Closed 8 years ago.
I was wondering if anyone could advise me a little. I am attempting to use JQuery to select my dropdown item by a given value. Here is what I have
var myVar = document.getElementById('<%= hiddenID.ClientID %>').value
if (myVar != "") {
var val = $("select[name*=drpDescription] option").filter(function () {
return ($(this).val() == myVar)
});
$("select[name*=drpDescription]").val(val.val());
}
The above code snippet does get the correct option based on it's value matching my hiddenfield value. What I can't do is get my dropdown to display it as the selected item. I wondered if anoyone has any advise on where I am slightly missing the point.
Thank you for any help.
You can set the value by passing it as parameter in .val() with select elements selector:
$("select[name*=drpDescription]").val(myVar);

Loop through Drop Down Option and read its attribute using jquery [duplicate]

This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it

Categories

Resources