Solved

prepending a variable in javascript

Posted on 2007-04-06
10
1,505 Views
Last Modified: 2012-05-05
Is there a function out there that will do this:

somevar="something"+somevar;

such as .prepend()? if there isnt, how would imake this function work like this:

somevar.prepend(),

NOT like this though:

prepend(somevar, prepend_value);


If possible, I want both answers so if i ever need to i can alter variables with functions without putting the variable i want to alter in the argument list.
0
Comment
Question by:Oscurochu
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18869564
see JavaScripts prototype syntax and JSON http://www.json.org/
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18869679
Check out the use of the EVAL function.  This function takes a string and executes it, which I believe is what you're looking for.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18877106
Why over-complicate things? What would this function gain you?

Obviously you know the value to prepend; why not just do as you suggested:

somevar="something"+somevar;
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 5

Author Comment

by:Oscurochu
ID: 18909630
using a function to prepend a variable will clear up a lot of code. i like to keep my code clean, so:


document.getElementById('blah').innerHTML="blah blah"+document.getElementById('blah').innerHTML


makes the code look cluttered. it would be easier to just use:

document.getElementById('blah').innerHTML.prepend('blah blah');
0
 
LVL 15

Expert Comment

by:stanscott2
ID: 18909647
In the example you just gave, you really want to insert HTML, so use this function with two argument, the node to be acted on, and the text to be added:

prepend('myNode','blah blah');

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
}

If you aren't sure that there will always be a firstChild node, then modify the function like this:

function prepend(nd, txt) {
     var curNode = document.getElementById(nd);
     if (curNode.firstChild) {
          curNode.insertBefore(document.createTextNode(txt),curNode.firstChild);
     } else {
          curNode.appendChild(document.createTextNode(txt));
     }
}
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18909706
i already said, i donot want it to work like that because of specific reasons i cant remember right at this moment. im trying to get it to work  like this, if possible:

document.getElementById('blah').innerHTML.prepend('some text');

innerHTML is just an example. if the above function is not possible, then i will just write a function that will use two arguments to do the work.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18910306
as I already said: JSON can do that
0
 
LVL 29

Expert Comment

by:Badotz
ID: 18910802
<$.02>Much ado about nothing</$.02>
0
 
LVL 5

Author Comment

by:Oscurochu
ID: 18911968
i dont understand how JSON works, can you help me out with that?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 20 total points
ID: 18956414
see prototype.js, something like:

String.extend(String.prototype, {prepend: function(str){ /* work here */ });

€.02 as asked for ;-)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question