I was not able to get part of this javascript code working for unknown reason and display as undefined. How do I merge vote[1] into the formObj which is document.forms[0] Any other alternate solution?
var elements2 = formObj.elements['vote[' + pollId + ']';
There is a basic syntax error:
var elements2 = formObj.elements['vote[' + pollId + ']';
Should be
var elements2 = formObj.elements['vote[' + pollId + ']'];
Could it be that you want:
var elements2 = formObj.vote[pollId];
(Assuming "vote" is the name of several form elements)
You might want to read about how to handle forms in JavaScript.
I'm not a javascript programmer really, but from what I can see above in the code you are missing a "]" at the end of elements.
It looks like your setting elements2 to formObject.elements[i] where you using vote[pollId] as the index. So vote[pollId] should return an integer in this scenario.
I'm not sure if I understand the question
Javascript Arrays
var formObj = document.forms[0];
var i = formObj.length + 1;
formObj[i] = vote[1];
Related
Trying to dynamically set variables depending how many vimeo iframes are on my page. Im using the Eval method in my code below:
var numberVimeoFrames = jQuery(".vimeo").length;
for(i=1;i<=numberVimeoFrames;i++){
var refFrame = jQuery('.vimeo:nth-child(' + i + ')');
eval("player" + i + "= new Vimeo.Player(" + refFrame + ")");
}
My eval line is however generating an error message:
Uncaught SyntaxError: Unexpected identifier
To me it looks like ive concatenated correctly so not sure where ive gone wrong?
Even though in this case I do not think it is that bad, the general opinion is to not use eval at all.
Use arrays instead :
var numberVimeoFrames = jQuery(".vimeo").length;
var players = [];
for(i=1;i<=numberVimeoFrames;i++){
var refFrame = jQuery('.vimeo:nth-child(' + i + ')');
players.push(new Vimeo.Player(refFrame));
}
You can now access your players by calling the array (for example players[1] instead of player1 and so on.)
I'm not sure exactly how much this question has something to do with ExtJS and how much with pure JavaScript. Anyways I have a string with comma separated value. I need to use for the GUI so I try to make it as user-friendly as I can. I made most of the things I wanted but one thing I can't accomplish yet. I want to replace all commas in the string with a proper image, which I think will fit very well on what I'm doing but for now - I try with no success.
For those familiar with ExtJS - I'm doing this for each cell in a certain column of a grid with a render function. But I think that maybe the problem must be solved with a pure JavaScript function. Here is what I have by now:
_cusomizeString: function(dates) {
if (dates != null)
{
var date = dates.replace(/,/g,"|");
var www = date.split('|');
var xxx = www.length;
for (var i = 2; i < xxx; i+=3)
{
www[i] = www[i] + '<br />';
}
var ggg = www.toString();
var hhh = ggg.replace(/,/g,'<img src =" ' + D:\dir1\dir2\dir3\dir4\dir5\img.png + ' "/>');
return hhh;
}
return dates;
}
I tried a few variations, now I don't get error but don't see an image either.
Thanks
Leron
P.S
With this change in the function:
var finalString = tempString.replace(/,/g,'<img src ="http://www.finishingtouch.co.uk/assets/images/common/calendar_icon.png"/>');
I am able to visualize this:
The main problem now is how to add the image before the first element, because now it's missing (Noticeable especially when there's only one date) and how I can make it work with local files for now? I've tried using this in my replace function:
'<img src ="file:///D:\\symapac\\src\\public\\img\\icons\\draft.png"/>'
But the console log returns this and I dont see no image:
07-06-2012<img src ="file:///D:\dir1\dir2\dir3\dir4\dir5\img.png"/>16-06-2012
Ok, I have almost final solution. Here is how it looks like:
Here is my final function:
_checkDates: function(dates) {
if (dates != null)
{
var date = dates.replace(/,/g,"|");
var arrayOfDates = date.split('|');
var stringLength = arrayOfDates.length;
for (var i = 2; i < stringLength; i+=3)
{
arrayOfDates[i] = arrayOfDates[i] + '<br />';
}
var tempString = arrayOfDates.toString();
var finalString = tempString.replace(/,/g," ,");
finalString = finalString.replace(/,/g,"<img src="+ "'" + pathToImage + "'" +"/>");
var imgSrc = "<img src="+ "'" + pathToImage + "'" +"/>";
var otuputString = imgSrc.concat(finalString);
return otuputString;
}
return dates;
}
There is that little problem that no matter now many tabs I put in var finalString = tempString.replace(/,/g," ,"); the space between the icons is always the same, no idea why. But that's the closest I get to what I've wanted.
Cheers
Leron
'<img src ="file:///D:/dir1/dir2/dir3/dir4/dir5/img.png"/>'
You have a space before your filename, also your filename isn't in quotes.
I am trying to add and remove things in a string with using arrays. However this following script I created is not working as it doesn't remove numbers that have been submitted:
function updateCCList(id)
{
var MemberClicked = '[' + id + ']';
var ListClickedMembers = document.frmSendMail.hidSenderList.value;
if(ListClickedMembers.indexOf(MemberClicked) == -1)
{
ListClickedMembers += MemberClicked;
}
else
{
ListClickedMembers = ListClickedMembers.replace(/' + MemberClicked + '/g,'');
}
alert(ListClickedMembers);
document.frmSendMail.hidSenderList.value += ListClickedMembers;
}
Any idea what is wrong?
Many thanks,
Paul
The main problem:
ListClickedMembers = ListClickedMembers.replace(/' + MemberClicked + '/g,'');
The first RegExp there looks bad. I think you mean new RegExp('\\['+id+'\\]')
In case you care about avoiding duplicate entries:
document.frmSendMail.hidSenderList.value += ListClickedMembers;
You don't need += there, = will suffice.
I have
$('#hidden-data').find('#' + $(this).attr('id')).html(currdata);
var hiddata = $('#hidden-data').find('#' + $(this).attr('id')).html();
I want to translate this to javascript but my brain is melted.
Please help ;p
The simplest version, given that IDs should be unique:
var hiddata = this.innerHTML = currdata;
The shortened jQuery version accounting for ID uniqueness makes this a bit more apparent:
$(this).html(currdata);
var hiddata = $(this).html();
Since you're taking the id attribute from this and no other element should have that ID, just use this, no need to find anything else in the DOM...you should already have the element.
var o = document.getElementById(thisid);
o.innerHTML = currdata;
var hiddata = o.innerHTML;
I'm pretty new to jQuery and Greasemonkey, but I want to reform a URL.
For example, given:
http://www.example.com/index.php?value1=blabla1&sid=blabla2&mid=blabla3
I want:
link://www.example.com/blabla1/data/blabla2/blabla3.ext
I tried code like this:
var sid=document.URL.substring(document.URL.indexOf('sid=')+15);
// How do I set the length of blabla2 ? -7 ?
Hopefully someone understands what I mean and can help me out a little.
Use regular-expression searches to get the values.
If you know the param names in advance, it's more straightforward than it looks...
var searchableStr = document.URL + '&';
var value1 = searchableStr.match (/[\?\&]value1=([^\&\#]+)[\&\#]/i) [1];
var sid = searchableStr.match (/[\?\&]sid=([^\&\#]+)[\&\#]/i) [1];
var mid = searchableStr.match (/[\?\&]mid=([^\&\#]+)[\&\#]/i) [1];
.
The last bit is then something like:
var domain = searchableStr.match (/\/\/([w\.]*[^\/]+)/i) [1];
var newlink = '//' + domain + '/' + value1 + '/data/' + sid + '/' + mid + '.ext';
.
.
PS: It's only slightly more work if you don't know the names in advance.
PPS: This is educational code. Beware of extra spaces and malicious data, when using in the wild.