for Loop is not Working inside Jquery [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This is working fine for first element but not for the whole table.
how can i iterate this for whole table.
my jquery code is like this
$(document).ready(function () {
for (i = 0; i <= 42; i++) {
if (true) {
$('#mktType').addClass("important blue");
} else {
$('#mktType').addClass("important red");
}
}
});

What you are doing is a nonsense, you are adding the same classes 42 times to the element with id "mktType".
If you want to add classes to several elements according to a condition, you should :
Use a class selector instead.
Use each to apply a funciton to each element you selected.
Specify a real condition, use it only in loop if it depends on the element itself.

Related

Storing a reference to an element dynamically added by append [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have added a new element dynamically through jQuery like this:
var elem = $('#unique').append($('<span>').attr('data-rollNo', i));
Now I need to use this element after this to add something to it. Is there a way I can store a reference to this element here, so I done need to search the entire DOM every time I edit this?
Use appendTo method instead of append:
var $span = $('<span>').attr('data-rollNo', i).appendTo('#unique');
Now, span is appended and you also have a reference to this new object.

How can I trigger .click() event from an object created using string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
var r="$(\""+"#table-trip tbody tr a[onclick=\'return FillTripDetails("+$('#<%=hdnPickLocationID.ClientID %>').val()+","+$('#<%= hdnDropLocationID.ClientID %>').val()+","+$('#<%= hdnTripID.ClientID %>').val()+")\']\")";
alert(r);
//$("#table-trip tbody tr a[onclick='return FillTripDetails(3,2,6)']").click();
r.click();
I want to call click event of element that is select in 'r' variable ?
How can i do it ?
Please advise
r is a string variable. To trigger the click, you would need selectors for selecting the dom and then jquery methods to trigger their events like click. Something like this:
$("#table-trip tbody tr a").click();
or
$("#table-trip tbody tr a").trigger('click');

Hyperlinks inside of a function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to put a link on "one" in the following code
Javascript
function myFunction(){
document.getElementById("text").innerHTML="text set one";
I will have multiple of these functions with different text so I need to be able to pick out text and attach links (that will lead to pop-up windows).
EDIT: I need the click able one to have something like
`"MM_openBrWindow('index.html','New_Window','width=500,height=500')"`
Maybe this makes your job easier:
function myFunction () {
document.getElementById("text").innerHTML = "text set " + "one".link("#");
}
function myFunction () {
document.getElementById("text").innerHTML = "text set <a href='#'>one</a>";
}
You can inlude a in innerHTML.
function myFunction(){
document.getElementById("text").innerHTML="text set <a href='#'>one</a>";
}

GetElementByClass of An Element Within Another Element [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I need to get the element by class of "balls" from the div gameContent.
Basically grabbing the lottery numbers from Play4 from this site:
http://www.flalottery.com/play4.do
How can I get the element by class from another class? If I just do balls, all of the numbers show up, which aren't relevant and would mess up data.
Do you mean something like this:
document.getElementsByClassName('gameContent')[0].getElementsByClassName('balls')
Get elements by class "gameContent" followed by "balls". Query assumes that the first gameContent is what we are interested in.
Hope this helps.
you can use the following query selector
var elems = document.querySelectorAll(".gameContent .balls")
That is pure JavaScript. You can of course use the same query selector for jQuery
For instance with jQuery this would be
var elems = $(".gameContent .balls")
Notice how the query selector is identical.
Did you try
$(".gameContent .balls")
Judgeing by the page you've included in your question, you'll probably want to iterate through the <span> elements to get each ball number:
$('.gameContent .balls').each(function(){
alert('next ball: '+$(this).html())
});

add listener to js created element. jquery ON not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to attach a listener to an element created via js dom manipulation. I thought that this is what jquery ON was for, but the below example is not working.
It works with the initial element, but not with any that are added via JS. The added elements have the correct class name.
<div id = "tag_options">
<div class = 'tag_option'>test</div>
</div>
function greet(event) { alert("Hello "); }
$("[class='tag_option']").on("click", {}, greet);
Try this:
function greet(event) { alert("Hello "); }
$("#tag_options").on("click", ".tag_option", greet);
Use delegation, e.g:
$(document.body).on("click","[class='tag_option']", greet);

Categories