开发者

how to pass Id to javascript dynamically

开发者 https://www.devze.com 2023-03-15 01:40 出处:网络
function jsarea3() { document.getElementById(\"Txtarea3\").title = document.getElementById(\"Txtarea3\").value; }
function jsarea3() { document.getElementById("Txtarea3").title = document.getElementById("Txtarea3").value; }
function jsarea4() { document.getElementById("Txtarea4").title = document.getElementById("Txtarea4").value; }
function jsarea5() { document.getElementById("Txtarea5").title = document.getElementById("Txtarea5").value; }

The code above is to show data in a textbox area on mouse hover. as you can see the code that run is same for all the 3 function the difference is only the ID How can I pass ID dynamically Like I can use the inner code as function as well for all the 3 and how to use it

and what about code like this the code given below

$('#button1').focus(function () {
      $('#button1', window.parent.document).css("back开发者_如何学编程ground-color", "#fcc63b"); 
    }
});


if you are using jquery you can select your textareas like this

$("textarea").focus(function(){
this.title = $(this).val();

});

you can specify the selector to be more specific to the items that you want to manipulate the function will be running when your textarea gain focus take a look at jquery it's simplify your code


Since you use the same elements title and value you can store a reference instead of searching for it twice..

function jsarea(anId) { 
  var elem = document.getElementById(anId);
  elem.title = elem.value;
}

and you can call it with jsarea('Txtarea3');

For the second code, it is jQuery code and it seems to change the background color of a button with the same id as the one that gets the focus in the parent window (when you use iframes)


You can use a single function which takes a parameter representing the id of the the element you wish to reference:

function jsarea(elementId) {
    document.getElementById(elementId).title = document.getElementById(elementId).value;
}

You can call this like jsarea("Txtarea3"); if you want to affect the element with id "Txtarea3".


Use a parameter.

In addition, you should take a look at jQuery - it can really simplify DOM-manipulating code.


function jsarea(number) { document.getElementById("Txtarea" + number).title = document.getElementById("Txtarea" + number).value; }
0

精彩评论

暂无评论...
验证码 换一张
取 消