본 사이트는 여러분의 사용자 경험을 개선하기 위해 유지 보수를 진행하는 동안 기능이 제한됩니다. 도움말로 문제가 해결되지 않고 질문을 하고 싶다면 Twitter의 @FirefoxSupport 및 Reddit의 /r/firefox 채널을 활용하세요.

Mozilla 도움말 검색

고객 지원 사기를 피하세요. 저희는 여러분께 절대로 전화를 걸거나 문자를 보내거나 개인 정보를 공유하도록 요청하지 않습니다. "악용 사례 신고"옵션을 사용하여 의심스러운 활동을 신고해 주세요.

자세히 살펴보기

cannot set param element value attribute using javascript setAttribute

  • 3 답장
  • 2 이 문제를 만남
  • 37 보기
  • 최종 답변자: cor-el

more options

When appending param elements via createFragment the setAttribute javascript function does not set the "value" attribute. It is necessary to set the nodetext to add the this value attribute to the param element.

Second when using the latter method the generated html sets the "value" to the innerHTML. The resulting HTML when view by "Inspect" looks like this.

<param id="myID">myValue</param>

Note: [the element close text </param> is correctly displayed using Inspect but when using File->SavePageAs the ending text is not saved correctly. I have reported this in another Moz. question in FF Help.]

function setparamAttrs(parmID, parmVal) {

var gData = document.createDocumentFragment();
 var newNode = document.createElement("param");
 var textStr = document.createTextNode(parmVal);
 newNode.appendChild(textStr);
 newNode.setAttribute("id", parmID);
 //newNode.setAttribute("value", parmVal);  // this does not work
 //newNode.setAttribute("innerHTML", parmVal);  //this does not work
 gData.appendChild(newNode);
 document.body.appendChild(gData);

}

I was, however, able to retrieve the "value" after it was appended using:

var val = document.getElementById("parm0").value);

getAttribute("value") does not work either.

Finally I added the param elements to my HTML as this; <param id="myID">myValue</param>. I then attempted to extract the value using document.getElementById("myID").innerHTML. No text was returned. When I examined the HTML document with Inspect my HTML param element was displayed as <param id="myID">myVal. The closing element text was missing the same as when I use SavePageAs that I previously reported.

When appending param elements via createFragment the setAttribute javascript function does not set the "value" attribute. It is necessary to set the nodetext to add the this value attribute to the param element. Second when using the latter method the generated html sets the "value" to the innerHTML. The resulting HTML when view by "Inspect" looks like this. <param id="myID">myValue</param> Note: [the element close text </param> is correctly displayed using Inspect but when using File->SavePageAs the ending text is not saved correctly. I have reported this in another Moz. question in FF Help.] function setparamAttrs(parmID, parmVal) { var gData = document.createDocumentFragment(); var newNode = document.createElement("param"); var textStr = document.createTextNode(parmVal); newNode.appendChild(textStr); newNode.setAttribute("id", parmID); //newNode.setAttribute("value", parmVal); // this does not work //newNode.setAttribute("innerHTML", parmVal); //this does not work gData.appendChild(newNode); document.body.appendChild(gData); } I was, however, able to retrieve the "value" after it was appended using: var val = document.getElementById("parm0").value); getAttribute("value") does not work either. Finally I added the param elements to my HTML as this; <param id="myID">myValue</param>. I then attempted to extract the value using document.getElementById("myID").innerHTML. No text was returned. When I examined the HTML document with Inspect my HTML param element was displayed as <param id="myID">myVal. The closing element text was missing the same as when I use SavePageAs that I previously reported.

선택된 해결법

Yes, that is the correct way to code this and it works for me with no problems.

문맥에 따라 이 답변을 읽어주세요 👍 0

모든 댓글 (3)

more options

You need to set the value attribute and not the innerHTML of a param if you want to inspect the value.

var val = document.getElementById("parm0").getAttribute("value"); 

function setparamAttrs(parmID, parmVal) {

 var gData =  document.createDocumentFragment();
 var newNode = document.createElement("param");

 newNode.setAttribute("id", parmID);
 newNode.setAttribute("value", parmVal);

 gData.appendChild(newNode);
 document.body.appendChild(gData);
}

setparamAttrs("param0", "test")
alert(document.getElementById("parm0").getAttribute("value"));
 
more options

The reason I was getting the innerHTML was due to the textNode being added. Whatever text is in the textNode becomes innerHTML. Once this was removed the dynamically added param element was displayed correctly and when I performed a SavePageAs the resulting HTML output was correct. Below is the working code. Please post after you confirm then I will mark as the chosen solution.

<html>
<body>

<h3>Param Fragment Demo</h3>


<param id='testParm' value='testValue'>		

<script language="javascript">

// begin main

alert(document.getElementById("testParm").value);

var gData = document.createDocumentFragment();

var newNode = document.createElement("param");
//var textStr = document.createTextNode(".");
//newNode.appendChild(textStr);
newNode.setAttribute("value", "fragValue");
newNode.setAttribute("id", "fragParm");

gData.appendChild(newNode);
document.body.appendChild(gData);

alert(document.getElementById("fragParm").value);

// end main

</script>
</body>
</html>

글쓴이 cor-el 수정일시

more options

선택된 해결법

Yes, that is the correct way to code this and it works for me with no problems.