use XML loaded number as a function variable - javascript

I need to use a variable from the code below in a link ID to load an overlay iframe. I'm loading numbers from an XML file that I'm using for each generated link parameter, I could use them as an ID too. Below is the javascript:
$(function(){
$('#b1').frameWarp();
});
Instead of using ID="b1", am I able to create variables based on the loaded XML numbers?
I cannot use class instead of ID.

Can you use:
$('#' + varName).frameWarp();
Apologies if this isn't what you're looking for. I'm having trouble understanding your question.

Related

Select parent div from inside the containing JavaScript

I've a lot of old cached html file which uses document.write.
There are multiple element like this in every html file
<div class="time"><script>timeago('2008-06-07 17:40:53')</script></div>
timeago function writes time using document.write and the function is defined in a separate js file. So It is easy to alter the function to some thing else without changing some 200K documents.
But the issue is, how can I rewrite that timeago function to select the containing div and print the time. Is it possible to select the div from that script which is contained in that div may be using jQuery or native JavaScript?
Any advice please
How about something like this?
<script>
function timeago(d) {
document.currentScript.parentElement.innerText = 'output = ' + d;
}
</script>
<div class="time"><script>timeago('2008-06-07 17:40:53')</script></div>

Modify HTML Content Created by 3rd Party JS Script

I am creating a custom sign in experience for a customer using okta Sign in Widget. As part of this 'widget' the function creates an HTML login form. One of the tags this generates I want to modify the contents of after it has been generated on page load by the Okta widget.
I've created a fiddle where I used the following code to modify the contents but it doesn't seem to be working.
$(document).ready(function(){
var headingClass = document.getElementsByClassName("okta-form-title");
headingClass.innerHTML = "<h2>Public Offers</h2>";
}) ;
Please could someone advise on how to get this working.
getElementsByClassName will give you an array of elements with that class name. So you need to iterate over it, or if you are sure there is only one element, use getElementsByClassName[0]
Example:
$(document).ready(function(){
var headingClass = document.getElementsByClassName("okta-form-title");
headingClass[0].innerHTML = "<h2>Public Offers</h2>";
}) ;
More information: https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName

Download html page and bind to a var

I'm new to javascript and jquery.
I wonder if there are any ways to download content of a target html page.
And bind the downloaded content to a variable , and later I can search for its tags inside or not.
Can anyone give me an answer please ? :)
Thank you
Yes, you can, using the jQuery 'load()' function: api.jquery.com
If you want to load it into a variable instead of an element, you cah use the 'get' function. After you loaded the html into a variable, you can wrap it to get a jQuery element.
A simple example (just pseudocode, copy/paste probably won't work):
$.get("/example.html", function( data ) {
var source = $(data);
//and now you have a jQuery element. You can use 'find' to seach the including tags
}, 'html');
Using HTMLAgilityPack to get the content from target html,like as
HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument doc = htmlWeb.Load(url);
doc.DocumentNode.SelectSingleNode(#"id('content')/div/div[1]/");
hopefully this help you.

Use callback function to create variables and use in a html element

Firstly excuse my ignorance with any inaccurate information I provide I a very new to javascript, jquery and json.
Anyway I have a script which pulls data from a json file and displays in a webpage with the help of javascript, jquery, ajax(i think) and json.
There is a callback for when I get back the results:
function searchCallback(data) {
$(document.body).append('<h1>' + data.title + '</h1>');
}
And it works fine the like this. However I want data.title (json object) to be displayed in a html element of my choice without having to use $(document.body) because my page won't display correctly at I have other html elements outside the script.
As far as I know (excuse ignorance) with javascript I can possible add a variable and use it as follows:
var title = data.title;
And in my html:
<span id="title"></span>
or maybe there is cleaner way?
Anyway how do I achieve this. Thank you for any help!!
If you want to find an element and modify, jQuery makes this easy. Instead of $(document.body).append find an existing element by it's id, and then call the text method on it to replace the text inside that element with something new.
$('#title').text(data.title);

Can you store a clicked img as a variable?

For my website I make the visitor choose between two different images. Is it possible to store the image that they chose as a variable? I then need to use that variable in an if conditional. Also is it possible to name each image as if it were a class?
Thanks!
Jenise
Best to use Javascript + jQuery:
var my_images = [];
$('img').click(function(){
my_images.push(this);
});
This will store the HTML DOM elements in an array for use later.
Highly recommend getting to grips with Javascript and then using jQuery (a Javascript library) for anything DOM related.
http://jquery.com/

Categories

Resources