Get text in getElementsByClassName [closed] - javascript

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 am writing some javascript to be run in a bookmarklet that should get the text within a element that has a certain class name.
So for example
document.getElementsByClassName('price')
where the web page has or example
<span class="price">
£23
</span>
How would I go about getting the actual text within the the element that has a class name price (i.e £23 in the above example).

getElementsByClassName returns an array of elements. Traverse through that array and get the innerText property of each. For example:
var priceEls = document.getElementsByClassName("price");
for (var i = 0; i < priceEls.length; i++) {
var price = priceEls[i].innerText;
alert("Price: " + price);
}
Live demo: http://jsfiddle.net/YQsBW/

Related

Jquery - else if to show text [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 8 years ago.
Improve this question
I have this code: http://jsfiddle.net/spadez/mn77f/6/
I want it to show a message when their are no fields (questions). I tried to implement this with the following addition:
} else if (x == 0) { < p > No additional questions < /p>
}
Which can be seen in the fiddle above. It however doesn't work and stops the whole thing working.
The log says: Unexpected token <
How should I have implemented this?
Simply change
< p > No additional questions < /p>
to
$("<p>No additional questions</p>").insertBefore(InputsWrapper);
jsFiddle update: http://jsfiddle.net/mn77f/7/
First of all you should put html strings in double or single quotes like this:
'<p>No additional questions</p>'
then create a paragraph element and give it an id, so that you can update its value to what you need or make it an empty paragraph if your validations have passed.
<p id="infoP"></p>
and then do this to update its value:
$('#infoP').html('<p>No additional questions</p>');
or this to set it to an empty paragraph:
$('#infoP').html('');

Looking for the Javascript of these JQuery lines of code [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'm currently learning HTML 5 + javascript, and one of the examples I'm looking at is written in JQuery. I realise that JQuery would be simpler to some people, but as I am just coming to terms with Javascript and no nothing of JQuery, I'm looking to get some lines of code translated from one to the other.
I know what these lines of code does, I'm just looking for it in JavaScript.
var showForm = function() {
if(editMode) {
var transaction = transactions[editMode];
amountField.value = transaction.amount;
if(transaction.type) $('[value='+transaction.type+']').attr('checked', 'true');
if(transaction.cleared) $('#cleared').attr('checked', 'true');
noteField.value = transaction.note;
dateField.value = transaction.date;
$('#time').val(transaction.time);
postcodeField.value = transaction.postcode;
searchField.value = ''
} else {
clearUI();
}
$('#formDiv').show();
$('#toolbar').hide();
$('#tableDiv').hide();
$('#map_div').hide();
$('#sum').hide();
Replace the lines where ever you select the element by ID with the corresponding ID
$('#cleared') ---> document.getElementById('cleared')
you can also use querySelector metod to access the element directly.
var cleared = document.querySelector('#cleared');
To show or hide a element you would need to set the style.display property
// To hide
cleared .style.display = "";
// To show
cleared .style.display = "block";
To get the element based on the attribute would be a bit of javascript..
$('[value='+transaction.type+']')
Where in you would need to iterate over each element and get the attribute of that element and then compare with the value.

Get the value of the array index [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 have a ajax response array and I want get the value of there index.
[{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}{"UserLatitude":"22.6962","UserLongitude":"75.8651"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"22.6962","UserLongitude":"75.8653"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3745"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}]
You're missing a comma after the first array object which is causing an error. Otherwise result[0].UserLatitude will work - jsfiddle.
You need to iterate the array and then check those values are matching like
$.each (arr, function (index, value) {
if(value["UserLatitude"] == "33.7543" && value["UserLongitude"] == "-84.3744") {
console.log(index);
return false;
}
});
if matches found, print the index and exit the loop.
Fiddle

How can I convert this coffeescript to js/jquery? [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
text = child.html()
text = text.replace /(#include(\s*<.*>)?)/gi, '<span>$1</span>'
text = text.replace /(main\(.*\))/gi, '<span>$1</span>'
child.html text
http://jsfiddle.net/dcro/XKHC8/
This is an answer from my question: Wrapping strings with a span I dont know how to use coffeescript and the one who answer looks unavailable.
From js2coffee.org
var text;
text = text.replace(/(#include(\s*<.*>)?)/gi, '<span>$1</span>');
text = text.replace(/(main\(.*\))/gi, '<span>$1</span>');
child.html(text);

How to replace occurrences by key [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
This is a simple question about JavaScript,
Say I got the following string:
A)My Name B)My Name C)My Name
now I give to a function the key #1 expecting to replace the second occurrence of My Name inside that function and return:
A)My Name B) C)My Name
I haven't found the solution to this anywhere online so I'm asking.
You could use split to separate the string into the parts, then join the before and after back together:
function removeNthMatch(input, removeString, removeIndex) {
var splitString = input.split(removeString);
result = splitString.slice(0, removeIndex + 1).join(removeString)
+ splitString.slice(removeIndex + 1).join(removeString);
}
input = "A)My Name B)My Name C)My Name";
removeString = "My Name";
removeIndex = 1;
console.log(removeNthMatch(input, removeString, removeIndex));

Categories

Resources