Solved

prepending a variable in javascript

Posted on 2007-04-06
10
1,511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

615 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