getElementByClass Split and then Display string + html - javascript

this is my first ever question and im sure its clear that I am new to this but hungry to learn! Any help is very appreciated.
I have a page where I need to grab the text of a class and then split it, displaying the first part [0] as a string and the end of the string 1 as an .
the string / iframe is separated by ##.
an example is
description description description ## <iframe>
currently the page displays it all as one long string.
as far as I have got is the follow:
var musicPlayer = document.getElementsByClassName(".program-description-text");
var fields = musicPlayer.split('##');
var myVideoDescription = fields[0];
var embedLink = fields[1];
Im sure this is wrong but assuming its not i still dont know how to display the results and how to call this to go grab and split the class string.
Thank you to anyone who takes the time to read this.
Edit:
I guess im so new that I can not find the right description for what I would like to achieve so I have taken some screen shots and edited one to show what I would like the result to be.

musicPlayer will likely be an array. You should try to console.log the musicPlayer and see what it contains.

Related

Replace "strings" with HTML code in contentEditable div

Idea
I'm making a configuration editor for a game, and I want to replace some of the strings with HTML code tooltips that would summarize what the command does, such as gravity or ammo.
I've tried data.replace(replacet, replacew), both (replace this) and (replace with) being array of strings and no, its not working.
JavaScript
function updateStrings() {
var replacet = "sv_cheats ";
var replacew = " <mark>sv_cheats</mark> ";
var data = document.getElementById('data').innerHTML;
document.getElementById('data').innerHTML = data.replace(replacet, replacew);
}
document.addEventListener('onkeydown', document.getElementById('data'), updateStrings());
Point
This code I'd say roughly works, but the caret position is set to 0 (beginning) on every key press as the JavaScript tries to append every character given at the first position which clearly is not supposed to be happening.
Question is are there any frameworks that do this automatically? I'd want something like in VS Code when if you hover over a function it tells you the parameters and such, something like that would be awesome to have!
Thanks!

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

How to use `%value%` in javascript?

I am learning JavaScript and I see %value% in a code but I do not know what does it mean or how to use it. Can anyone please help me explain to me. Thank you very much.
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
"%data%" is just a literal string. This code will take the value of HTMLWorkLocation, look for the first occurrence of %data% in it, and replace that with the value of work.jobs[job].location, and store the resulting string in formattedLocation.
var work = {
jobs: [{
location: "Home office"
}]
};
var job = 0;
var HTMLworkLocation = "John is located at %data%";
var formattedLocation = HTMLworkLocation.replace("%data%", work.jobs[job].location);
console.log(formattedLocation);
This is probably part of a template system that's used to replace placeholders like %data% with values that come from a table.
You're using string.replace which takes a string or regular expression as it's first argument. Based on the code you posted it looks like you're looking for the string "%data%" (or whatever string you're looking for) in HTMLworkLocation and replacing it with the value in work.jobs[job].location. Then it is being stored in formattedLocation.
I would put a debugger; line after that line of code so you can see what the values are in the debugger console. That might help make more sense of things.
Here is more info on the str.replace method with some examples

Javascript "Replace" not working on string returned via jQuery

I'm trying to convert a currency string to a number. I'm using a replace function with a regexp that I've used successfully in a similar context before.
The currency string is captured here, in part of an "each" loop:
var unitGridPrice = jQuery(this).find(".clsPriceGridDtlPrice").html();
The result is that unitGridPrice is a currency string, something like "$2.75". I'm trying to convert it to a number here:
var priceToConvert = unitGridPrice;
var unitGridPriceNo = Number(priceToConvert.replace(/[^0-9\.]+/g, ''));
However with that last line in place, the script will not run.
If I use the value of priceToConvert it correctly displays the currency text string, so I believe the string feeding the replace function is correct.
if I change "var priceToConvert = unitGridPrice" to "var priceToConvert = "$2.75" the script properly returns 2.75. I can copy and past the value that unitGridPrice displays into the text string I'm testing with and it works, but with the variable there the script dies.
I've tried removing the regex, changing the replace to .replace('$', '') and again the script stops with the variable in place but works if I test with a fixed string.
I'm really stumped. Help??!! Thank you!!!
i had some problem while try to get number from string also, little time ago. the problem is the regex, so i changed the regex like code below.
var id = element.name.replace ( /[^\d.]/g, '' );
element.name above is like input_21,input_22, etc. and i wanna get only the number(21,22).
hope it can help you. :)

Javascript: Insert text at specified line

I am kind of new to javascript, but I was looking to see if it was possible to add a line of text to a file at a specified position? i.e. every file needs to have the same text inserted at line no. 3
Someone suggested "split by line feed, add data, then rejoin", but i have no clue how to do that. Just looking for somewhere to get started or pointed in the right diretion.
Split by line feed:
data = data.split(/\r?\n/);
Add data:
data = data.splice(2, 0, new_data);
Rejoin:
data = data.join("\r\n");

Categories