/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] create elements for image displays

I’m trying to use an ajax function to load filenames into a text area. This works and is not the problem.

Then I read the contents of the text file and create an array of file images.
The images can be .jpg, .gif, etc. Again no problem.

However, when I try to create new <img …> elements with a ‘createElement’ function, I get major errors.

I think the problem is in the ‘AddImage()’ function in the code below.

Any ideas how to fix this? 😮

[code]
<html>
<head>
<title>Image Display</title>
<script type=”text/javascript” src=’ajaxcode.js’>
// From: http://www.hunlock.com/blogs/Ten_Javascript_Tools_Everyone_Should_Have
</script>

<script type=”text/javascript”>
// Just a stub function we’ll tell ajaxObject to call when it’s done
// callback functions get responseText, and responseStat respectively in their arguments.
// function fin(responseTxt,responseStat) { alert(responseStat+’ – ‘+responseTxt); }

function fin(responseTxt,responseStat) {
if (responseStat == 200) {
document.getElementById(‘TArea’).value = responseTxt;
} else { alert(‘Unable to load error: ‘+responseStat); }
}

function test(fname) {
if (fname == ”) return;
var test = new ajaxObject(fname,fin);
test.update();
}
</script>
<script type=”text/javascript”>
function AddImage(imgName) { alert(imgName);
var myLink = document.createElement(‘img’);
var mySrc = document.createAttribute(‘src’);
myLink.setAttribute(mySrc,imgName);
var myHigh = document.createAttribute(‘height’);
myLink.setAttribute(myHigh,’75px’);
var myWide = document.createAttribute(‘width’);
myLink.setAttribute(myWide,’75px’);
var spanAppend = document.getElementById(‘imgArea’);
spanAppend.appendChild(myLink);
}

function ShowImages() {
var str = ”;
var imgArr = document.getElementById(‘TArea’).value.split(‘n’);
for (var i=0; i<imgArr.length; i++) { AddImage(imgArr[i]); }
// document.getElementById(‘imgArea’).innerHTML = str;
}

</script>
</head>
<body>

Files to display
<select onchange=”test(this.value)”>
<option value=””>Select file</option>
<option value=”./tmp.txt”>2014</option>
</select>
<button onclick=”ShowImages()”>Show</button>

<br>
<textarea id=”TArea” rows=”10″ cols=”50″></textarea>
<div id=”imgArea”>

</div>
</body>
</html>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Gozzy82Mar 04.2010 — hey JMRKER

if your using document.createAttribute you have to use setAttributeNode instead of setAttribute and you have to set the value of the attribute through .nodeValue = '';

(https://developer.mozilla.org/En/DOM/Document.createAttribute)

here it works: http://www.webdevout.net/test?019&raw

in short using setAttribute('src', filename); is a lot easier than using document.createAttribute ?
Copy linkTweet thisAlerts:
@JMRKERauthorMar 04.2010 — Thanks for the information 'Gozzy82'. I'll give those sites a peek! ?

I'm OK with existing elements but, as you can probably tell, I'm new to the dynamic creation of them. :o
Copy linkTweet thisAlerts:
@JMRKERauthorMar 04.2010 — After reading the links provided by 'Gozzy82', I ended up with this function that seems to work best.
<i>
</i>function AddImage(imgName) {
if (imgName == '') { return; }
var oImg = document.createElement('img');
oImg.setAttribute('src',URL+imgName);
oImg.setAttribute('height','75');
oImg.setAttribute('width','75');
document.getElementById('imgArea').appendChild(oImg);
}

where URL is a global variable for the directory of images to display.

Thanks again. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...