Solved

prepending a variable in javascript

Posted on 2007-04-06
10
1,502 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now