Set Different JS variable from PHP foreach loop [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have very annoying problem, I need to display MySQL queries by using a PHP foreach loop but because of this when i try to set a javascript value for each of the items that are looped through it only selects either the first or last value it ignores the rest.
Here's what i've tried so far:
I have given an input a class and given it a value as well, this is within the for-each loop.
<input class="classs" value="{{ $item->id }}"/>
Next i have tried to access this from a js function, which is outside of the for-each loop.
function addSubTaskToDatabase(){
var itemId = $('.class').val();
alert(itemId);
}
Problem is all this does is alert the last value in the for-each loop with that class, its the same for name and for id also.
Anyone know the fix..? all help is appreciated.

var itemId = $('.class').val();
is going to select all elements on the page with that class.
You probably want a click function on the elements, and use this to access the currently-clicked item:
$('.class').click(function() {
var itemId = $(this).val();
alert(itemId);
});

To get all values you need an $().each loop
$('.class').each(function() {
var itemId = $(this).val();
alert(itemId);
}
see https://api.jquery.com/each/

Related

how to pass a variable to queryselectorAll? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.

assigning same html elements to a multiple variable in jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am curious to know, how can i assign multiple html elements to a single variable in jquery...
for an example if i am having...
$('.some').drags();
$('.some1').drags();
at one instance i want variable drgoff
var drgoff = $('.some').offset().top;
at other instance i want variable drgoff
var drgoff = $('.some1').offset().top;
i am using this drgoff in the function, so now my question is how can i get all that html elements in place of .some when that particular html element is called...
var drgoff is not inside function it is a global variable..
thanx for any help...
it can be done using if else also but that will be too lengthy..
Use jQuery's .add(). var drgoff = $('.some').add('.some1');
Live demo here (click).
Well don't know when you are setting the value for drgoff. If you have a function (as indicated in question), then you can pass it the class name for which you want to get the value like this:
function getDragOffValue(cname){
dragoff = $(cname).offset().top;
}
and use it like:
getDragOffValue(".name");
alert(dragoff);//gets value for elements with class .name
getDragOffValue(".name1");
alert(dragoff);//gets value for elements with class .name1
But I don't understand how your code will behave if you have multiple elements with same class name. It will return the offset().top value for first element in collection of elements with given class. In short $(".name").offset().top is as good as $(".name:first").offset().top.

Easier multiple value changes with jQuery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am really in need of this one, because I cann't manually type loads of options for admin panel. Hence I need a quick way to fetch each input/select and add value as I want.
Here's looong sample for one option field:
$("select[name=big_proceed]").val("true");
$("select[name=proceed_action]").val("");
$("input[name=choice_premium]").val("Premium <span>Code</span>");
$("input[name=big_proceed_pts]").val("");
$("#settings_proceed input, #settings_proceed select").each(function () {
databaseData($(this));
});
I thought something like this may work but apparently I was wrong. Here's sample:
$("#settings_proceed :input").each(function () {
$(this)
.eq(0).val("true")
.eq(1).val("")
.eq(2).val("Premium <span>Code</span>")
.eq(3).val("");
databaseData($(this));
});
Any suggestions for me ?
From the jQuery documentation:
.eq(index): Reduce the set of matched elements to the one at the specified index.
Hence your second example doesn't work as intended because $(this) only matches one element (that's the intention behind the .each()). You could rewrite the code like so:
var values = ["true", "", "Premium <span>Code</span>", ""];
$("#settings_proceed :input").each(function(i){
$(this).val(values[i]);
databaseData($(this));
});
However, this approach makes the code hard to read and error-prone because it assumes a fixed order of the HTML elements (what if you change the order of the input fields but forget to adjust the JS accordingly?). So you really should "manually" add IDs to your input elements and select them by their ID and not their index.
As #David Thomas pointed out, some sample HTML would be quite helpful here, but without knowing any further details of what you're trying to do I'd suggest the following:
var values = {
big_proceed: "true",
proceed_action: "",
choice_premium: "Premium <span>Code</span>",
big_proceed_pts: ""
};
$.each(values, function(key, value){
$("#settings_proceed").find("[name='"+key+"']").val(value);
databaseData($(this));
});
That way you can neatly define all the values in one object and let jQuery safely do the rest.

How to apply substring to every letter in array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making an array of textstrings like this:
phone = $(data).find('.tel a')
I would like to apply a substring(8) to every item in the array called phone. Is a for-loop the best way to do it?
phone, as it stands, contains a jQuery object, which is an Array-like Object of DOM elements. If you want to iterate over all of them and get their inner text, applying .substring(8) to each, and building an array out of them, you can use something like this:
var phoneArray = $(data).find(".tel a").map(function (i, el) {
return $(el).text().substring(8);
}).get();
DEMO: http://jsfiddle.net/96HWv/
(in the demo, I had to emulate what data could be, although I'm guessing it is an HTML string in your real code)
You can use the map() method :
phone = phone.get().map(function(e) { return $(e).text().substring(8) });
FIDDLE
You can use the .each() function for this... Something like:
$(data).find('.tel a').each(function() {
$(this).text(function(index,text) {
return text+"substring(8)";
});
});
You can let jQuery do the work for you.
$(data).find('.tel a').addClass('substring');
jQuery will traverse the array of elements returned and add the class to all of them.

Retrieve value from input object plain js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have the following field,
<input id="department" value="finance"/>
I'm trying to create an object variable containing the field in plain old javascript like so.
var $field = $(document.getElementById("department"));
I'm then trying to pull the value from the field variable, however it's failing.
$field.value;
Does anybody know what I'm doing wrong?
That looks like a jQuery object, which is a collection of DOM elements, and the value property is attached to the DOM elements.
You should have:
var field = document.getElementById("department");
// now you can access field.value
jQuery objects have a val() method that gets you the value of that property:
var field = $('#department');
// here you can call field.val();
Lose the call to $() if you want a DOM element, and not a jQuery object:
$field = document.getElementById("department");

Categories

Resources