Showing posts with label dom. Show all posts
Showing posts with label dom. Show all posts

Tuesday, July 23, 2019

[JavaScript][Resolved] Javascript click radiobox by value

<h1>st=0 </h1>
<input type="radio" name="st" value="0" /> 0 <br />
<input type="radio" name="st" value="1" /> 1 <br />
<input type="radio" name="st" value="3" /> 3 <br />
<input type="radio" name="st" value="4" /> 4 <br />
<input type="radio" name="st" value="5" /> 5 <br />

<script>
function clickRadioByValue(radioName,value){
    var nodes = document.querySelectorAll('input[name="'+radioName+'"]');
    for(var k in nodes){
 if(nodes[k].value == value){
            nodes[k].setAttribute("checked","checked");
        }
    }
}
clickRadioByValue("st",4);
</script>

Result :

you can try change the value 4 to 0,1,3 or 5 in clickRadioByValue("st",4); to test

Monday, July 15, 2019

[JavaScript][Dom][Example] Create image node

I searched there are 2 method to create img node with js, you can create an Image() object or simply create element.
<script>
<div id="content"></div>
<script>
var img1= new Image();
img1.src="https://mesh-uploads-legacy.s3.amazonaws.com/mesh-control/d35d13d303ca403e847bd587e514b87e_size-logo.png";
document.getElementById("content").appendChild(img1);
var img2 = document.createElement("img");
img2.src="https://mesh-uploads-legacy.s3.amazonaws.com/mesh-control/d35d13d303ca403e847bd587e514b87e_size-logo.png";
document.getElementById("content").appendChild(img2);
</script>

Result :